0

我目前正在尝试编写一个 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

它远未完成,这只是我遇到的问题,可能它可以做得更紧凑,但我会在它完成时研究它:)

4

1 回答 1

3

编辑这是折叠form2中所有代码的一种方法:

OValShape 可能有一个名为 TAG 的属性,将Form2上的属性设置为“蓝色”、“红色”等。然后:

Private Sub OvalShape1_Click(sender As Object, e As EventArgs) _
     Handles OvalShape1.Click, OvalShape2.Click, _
     'Handles OvalShape3.Click ... (add a handles clause for each oval, 
     '     ' 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
 frmOther.OvalShape1.BackColor = Color.FromName(oval.Tag)

End Sub  

编辑

假设您的应用程序从 FormMain 开始,即 Form1,我们需要为它创建一个公共引用。在表格 1 中:

Public Shared frmMain As Form1

在您的 FormLoad 事件中:

Sub Form_Load (......) handles Me.Load
   frmMain = Me
End Sub

现在,Form2 代码将使用 frmMain 作为实例引用。

编辑 3

这就是 Mark 的想法:与其让 Form2 和 form1 一样玩弄,不如给 Form1 暴露一个属性来自己设置颜色。在表格 2 中:

Public SelectedColor As Color

' slight change here
Private Sub OvalShape1_Click(sender As Object, e As EventArgs) _
      Handles OvalShape1.Click, Handles OvalShape2.Click
      ' add a HANDLES for each Oval click event so you dont have to 
      ' copy this code to all of them, but DELETE the old ones.
      ' your code looks like you added the multiple HAndles to that click (prematurely)


 ' sender is still whichever OVal was clicked:

 Dim oval as OvalShape = Sender
 SelectedColor =  Color.FromName(oval.Tag)

 Me.DialogResult = DialogResult.OK
 me.Close


End Sub  

在表格 1 中:

Private Sub OvalShape1_Click(sender As Object, e As EventArgs) Handles OvalShape1.Click
       'sender is the oval shape clicked, so
   dim oval As OvalShape = sender
   dim oClr as Color = oval.BackColor

   ' Form2 is a CLASS or a template for a form...need to make an instance
   ' of Form2 to show:

   dim frm as New Form2(oClr)             ' the right way

   ' kind of pointless since we only return OK
   if frm.ShowDialog=DialogResult.Ok then

      OvalShape1.BackColor = frm.SelectedColor        ' get the color selected
   end if


End Sub
于 2013-10-12T17:32:51.210 回答