我目前正在尝试编写一个 mastermind 版本。我为引脚使用椭圆形,并且我设置了当您单击椭圆时,它会显示包含颜色选择的 form2。我正在寻找某种代码,它可以查看单击了哪个椭圆并使用它将选定的颜色变为正确的椭圆。我知道我可以通过为我拥有的每个椭圆添加一个表格来做到这一点,但我认为那里必须有更好的解决方案:)
代码:
Imports Microsoft.VisualBasic.PowerPacks
Public Class Form1
Dim pc1 As Integer
Dim pc2 As Integer
Dim pc3 As Integer
Dim pc4 As Integer
Public Shared frmMain As Form1
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
frmMain = Me
Randomize()
pc1 = Math.Round(Rnd() * 5) + 1
pc2 = Math.Round(Rnd() * 5) + 1
While pc2 = pc1
pc2 = Math.Round(Rnd() * 5) + 1
End While
pc3 = Math.Round(Rnd() * 5) + 1
While pc3 = pc1 Or pc3 = pc2
pc3 = Math.Round(Rnd() * 5) + 1
End While
pc4 = Math.Round(Rnd() * 5) + 1
While pc4 = pc1 Or pc4 = pc2 Or pc4 = pc3
pc4 = Math.Round(Rnd() * 5) + 1
End While
showpc()
End Sub
Sub showpc()
If pc1 = 1 Then
OvalShape41.BackColor = Color.Blue
ElseIf pc1 = 2 Then
OvalShape41.BackColor = Color.Red
ElseIf pc1 = 3 Then
OvalShape41.BackColor = Color.Lime
ElseIf pc1 = 4 Then
OvalShape41.BackColor = Color.Yellow
ElseIf pc1 = 5 Then
OvalShape41.BackColor = Color.Black
ElseIf pc1 = 6 Then
OvalShape41.BackColor = Color.White
End If
If pc2 = 1 Then
OvalShape42.BackColor = Color.Blue
ElseIf pc2 = 2 Then
OvalShape42.BackColor = Color.Red
ElseIf pc2 = 3 Then
OvalShape42.BackColor = Color.Lime
ElseIf pc2 = 4 Then
OvalShape42.BackColor = Color.Yellow
ElseIf pc2 = 5 Then
OvalShape42.BackColor = Color.Black
ElseIf pc2 = 6 Then
OvalShape42.BackColor = Color.White
End If
If pc3 = 1 Then
OvalShape43.BackColor = Color.Blue
ElseIf pc3 = 2 Then
OvalShape43.BackColor = Color.Red
ElseIf pc3 = 3 Then
OvalShape43.BackColor = Color.Lime
ElseIf pc3 = 4 Then
OvalShape43.BackColor = Color.Yellow
ElseIf pc3 = 5 Then
OvalShape43.BackColor = Color.Black
ElseIf pc3 = 6 Then
OvalShape43.BackColor = Color.White
End If
If pc4 = 1 Then
OvalShape44.BackColor = Color.Blue
ElseIf pc4 = 2 Then
OvalShape44.BackColor = Color.Red
ElseIf pc4 = 3 Then
OvalShape44.BackColor = Color.Lime
ElseIf pc4 = 4 Then
OvalShape44.BackColor = Color.Yellow
ElseIf pc4 = 5 Then
OvalShape44.BackColor = Color.Black
ElseIf pc4 = 6 Then
OvalShape44.BackColor = Color.White
End If
End Sub
Private Sub OvalShape1_Click(sender As Object, e As EventArgs) _
Handles OvalShape1.Click, OvalShape2.Click, OvalShape3.Click, OvalShape4.Click
' delete all the other click events or remark them out
' sender is still whichever OVal was clicked:
Dim oval As OvalShape = sender
' FORM1 is not correct, need the instance name
Me.OvalShape1.BackColor = Color.FromName(oval.Tag)
End Sub
End Class
Public Class Form2
Private Sub OvalShape1_Click(sender As Object, e As EventArgs) Handles OvalShape1.Click
Form1.OvalShape1.BackColor = Color.Blue
End Sub
Private Sub OvalShape2_Click(sender As Object, e As EventArgs) Handles OvalShape2.Click
Form1.OvalShape1.BackColor = Color.Red
End Sub
Private Sub OvalShape3_Click(sender As Object, e As EventArgs) Handles OvalShape3.Click
Form1.OvalShape1.BackColor = Color.Lime
End Sub
Private Sub OvalShape4_Click(sender As Object, e As EventArgs) Handles OvalShape4.Click
Form1.OvalShape1.BackColor = Color.Yellow
End Sub
Private Sub OvalShape5_Click(sender As Object, e As EventArgs) Handles OvalShape5.Click
Form1.OvalShape1.BackColor = Color.Black
End Sub
Private Sub OvalShape6_Click(sender As Object, e As EventArgs) Handles OvalShape6.Click
Form1.OvalShape1.BackColor = Color.White
End Sub
End Class
它远未完成,这只是我遇到的问题,可能它可以做得更紧凑,但我会在它完成时研究它:)