title: See which item is calling a form
body: <p>I am currently trying to program a version of mastermind. I use oval shapes for the pins and I have it set up that when you click an oval it shows form2 containing the color selection. I am looking for some kind of code that will make it possible to see which oval was clicked and use that to get the selected color to the right oval. I know I could do it by adding a form for each oval I have, but I think there has to be a better solution out there :)</p>
  
  <p>Code:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>It is far from complete, this is just something I ran into, probably it can be made much more compact but I will look into that when it's finished :)</p>