【问题标题】:how would i change the color of an array element if it meets a condition visual basic如果满足条件visual basic,我将如何更改数组元素的颜色
【发布时间】:2021-07-31 13:24:06
【问题描述】:
Public Class Form1

    Dim r, r1, g, g1, b, b1 As Integer
    Dim grid(0 To 9, 0 To 9) As Label

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        timer1.Enabled = False
        Randomize()

        For row = 0 To 9
            For col = 0 To 9
                grid(row, col) = New Label

                grid(row, col).Width = 50
                grid(row, col).Height = 50
                grid(row, col).Left = 55 * col + 55
                grid(row, col).Top = 55 * row + 55
                grid(row, col).BackColor = Color.White

                Me.Controls.Add(grid(row, col))

                AddHandler grid(row, col).MouseEnter, AddressOf mouse_enter
            Next
        Next
    End Sub

    Private Sub mouse_enter(sender As Label, e As EventArgs)

        r = Int(Rnd() * 256)
        g = Int(Rnd() * 256)
        b = Int(Rnd() * 256)

        For row = 0 To 9
            For col = 0 To 9
                sender.BackColor = Color.FromArgb(r, g, b)
            Next
        Next
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Dim row As Integer
        Dim col As Integer

        For row = 0 To 9
            For col = 0 To 9
                r = Int(Rnd() * 256)
                g = Int(Rnd() * 256)
                b = Int(Rnd() * 256)

                grid(row, col).BackColor = Color.FromArgb(r, g, b)
            Next
        Next
    End Sub

    Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
        Timer1.Enabled = True
        Timer1.Interval = 800
    End Sub
End Class

我想创建一个 10x10 的白框数组,一旦鼠标移过它们就会变成随机的 rgb 颜色,效果很好。但是,我还希望已经着色的框在每个 timer1 滴答声中都变成一个新的 rgb 值。使用上面的代码,它只是在每次刻度时更改数组中的所有 100 个框,无论它是否被着色(用户用鼠标浏览它)。有谁知道我将如何完成这项工作?

【问题讨论】:

  • 首先,您需要确定您的实际问题是什么。这真的与改变任何东西的颜色无关。你显然已经知道如何做到这一点。它甚至与数组没有任何关系,因为您显然可以访问一个元素。问题在于确定您是否已经对Label 采取了行动。
  • 很明显,当你想做某事,如果满足特定条件,你需要一个If 语句。该语句需要测试您想要满足的条件。那是什么条件?这就是您需要考虑的:测试所需的条件并在它是True 时采取行动。如果没有可以轻松测试的条件,则创建一个,例如将Label 的Tag 设置为第一次操作时的特定值,然后您可以稍后查找该值以确定它是否已被操作。
  • 我认为这个问题完美地展示了初学者最大的问题,即他们试图直接从最终结果的想法编写代码,而不是先弄清楚逻辑,然后编写代码来实现那个逻辑。如果您尝试编写代码而不知道它必须做什么,那么它不太可能做到。如果这是一个手动过程,你会怎么做?您查看每个标签并检查它是默认颜色还是其他颜色,如果是其他颜色,您将做任何需要做的事情。为什么要改变,因为你是在代码中做的?
  • @jmcilhinney 我试过做(我不在家,所以我想这是我放的)``` 如果 grid(row,col).BackColor.Equals(Color.White) = False然后 For row 0 To 4 For col 0 To 4 grid(row,col).BackColor = Color.FromArgb(r,g,b) Next Next End If ``` 我知道我需要一个 if 语句来检查条件,我只是不知道如何检查网格数组中每个元素的实际情况
  • “我只是不知道如何检查网格阵列中每个元素的实际情况”。你当然知道。想想看。如果你手动做,你会怎么做?您一次只能检查一个网格项目,不是吗?您将遍历网格并一次处理一个项目,那么,为什么这会有所不同?你已经在这样做了。您已经嵌套了For 循环来遍历网格并一次访问一项。您已经设置了该项目的BackColor,那么为什么不能先检查该项目的当前值?

标签: arrays vb.net visual-studio


【解决方案1】:

“我还希望已经着色的框在每个 timer1 滴答声中都变成一个新的 rgb 值。”由于每次计时器计时都会发生这种情况,因此If 语句将属于Timer1.Tick 事件。如果您希望鼠标输入标签后颜色保持不变,请以相同的方式将If 添加到mouse_enter 代码中。

您可以检查标签的.BackColor 是否仍然是White,但是如果mouse_enter 中随机选择的.BackColor Sub 恰好是White 怎么办?你还想改变它吗?可能最好使用标签的.Tag 属性。

当调用mouse_enter 时,将.Tag 属性设置为"Changed"。然后在.Tick 事件中检查.Tag 属性是否仍然是Nothing。

虽然旧的 vb6 Rnd 函数可以工作,但最好使用 .net Random 类。在表单级别创建此类的单个实例。

我将获取随机颜色的代码移到了单独的函数中,因此我们不需要重复代码。

Private grid(0 To 9, 0 To 9) As Label
Private Rand As New Random

Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Timer1.Enabled = False
    For row = 0 To 9
        For col = 0 To 9
            grid(row, col) = New Label
            grid(row, col).Width = 50
            grid(row, col).Height = 50
            grid(row, col).Left = 55 * col + 55
            grid(row, col).Top = 55 * row + 55
            grid(row, col).BackColor = Color.White
            Controls.Add(grid(row, col))
            AddHandler grid(row, col).MouseEnter, AddressOf mouse_enter
        Next
    Next
End Sub

'The signature of this method must match the signature of Label.MouseEnter
'So sender must be As Object, not As Label
Private Sub mouse_enter(sender As Object, e As EventArgs)
    'Not all Objects have a .BackColor property so we must Cast to the underlying type, Label
    Dim l = DirectCast(sender, Label)
    l.BackColor = GetNewColor()
    l.Tag = "Changed"
End Sub

Private Function GetNewColor() As Color
    Dim r = Rand.Next(256)
    Dim g = Rand.Next(256)
    Dim b = Rand.Next(256)
    Dim NewColor = Color.FromArgb(r, g, b)
    Return NewColor
End Function

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    For row = 0 To 9
        For col = 0 To 9
            If grid(row, col).Tag Is Nothing Then '.Tag has never been set
                grid(row, col).BackColor = GetNewColor()
            End If
        Next
    Next
End Sub

Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
    Timer1.Enabled = True
    Timer1.Interval = 800
End Sub

【讨论】:

    猜你喜欢
    • 2020-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 2017-05-07
    相关资源
    最近更新 更多