查看哪个项目正在调用表单

时间:2013-10-12 17:13:49

标签: vb.net forms

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

它远非完整,这只是我遇到的事情,可能它可以做得更紧凑但我会在它完成时调查:)

1 个答案:

答案 0 :(得分:3)

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

OValShape可能有一个名为TAG的属性,将 Form2 上的属性设置为'Blue','Red'等。然后:

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开始,我们需要为它做一个公共引用。在Form1中:

Public Shared frmMain As Form1

在FormLoad事件中:

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

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

编辑3

这是马克的想法:我们将为Form1公开一个属性,而不是Form2弄乱form1的玩具。在Form2中:

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  

在Form1中:

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