[英]MongoDB - How can I check if some element in an array meets a condition with another element of an array?
[英]how would i change the color of an array element if it meets a condition visual basic
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 個框,無論它是否被着色(用戶用鼠標瀏覽它)。有誰知道我將如何完成這項工作?
“我還希望已經着色的框在每個 timer1 滴答聲中都變成一個新的 rgb 值。” 由於每次計時器滴答時都會發生這種情況,因此If
語句將屬於Timer1.Tick
事件。 如果您希望鼠標輸入 label 后顏色保持不變,則以相同的方式將If
添加到mouse_enter
代碼中。
您可以檢查標簽的.BackColor
是否仍然是White
但是如果mouse_enter
Sub
中隨機選擇的.BackColor
恰好是White
怎么辦? 你還想改變它嗎? 可能最好使用 Label 的.Tag
屬性。
調用mouse_enter
時,將.Tag
屬性設置為"Changed"
。 然后在.Tick
事件中檢查 .Tag 屬性是否仍然是Nothing
。
雖然舊的 vb6 Rnd
function 會工作,但最好使用 Z2D50972FECD376129545507F1062089Z Random
ZA2F212ED4F8EBC42ABC1DDZ4C。 在表單級別創建此 class 的單個實例。
我將獲得隨機顏色的代碼移動到單獨的 function 中,因此我們不需要重復代碼。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.