title: How Can i target one of my pictureboxes created on runtime? VB.NET
body: <p>So basically, i have successfully randomized certain pictureboxes in a grid to contain mines, and show that mine, and for testing purposes, those mines are currently showing. What do you think I need to do to be able to say:</p>
  
  <blockquote>
    <p>If, you click this box and mine = 1 (there's a mine), then you lose. <br>
    Else, keep going.</p>
  </blockquote>
  
  <p>simple enough, but i want to apply this to all boxes, no matter how big the grid is. (ZonesX * Zones Y)</p>
  
  <p>The furthest I have gotten is being about to pop up a MsgBox() when anyone of them is clicked.</p>
  
  <p>This is all created on runtime. Heres my code.</p>
  
  <pre><code>Public Class Form1
  Inherits System.Windows.Forms.Form
  
  Dim images(8) As Image 'declares image array
  
  Dim zonesY As Integer = 10
  Dim zonesX As Integer = 10
  
  Dim Guy As Object
  Dim pbxNewZone As PictureBox = DirectCast(Guy, PictureBox)  'declares pbxNewZone as a picturebox variable
  
  Dim generator As New Random
  
  Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
      images(0) = Image.FromFile("blank.png")
      images(1) = Image.FromFile("1.png")
      images(2) = Image.FromFile("2.png")
      images(3) = Image.FromFile("3.png")
      images(4) = Image.FromFile("4.png")
      images(5) = Image.FromFile("5.png")
      images(6) = Image.FromFile("clear.png")
      images(7) = Image.FromFile("hit.png")
      images(8) = Image.FromFile("mine.png")
  
      Dim x As Integer  'declares x as an integer variable
      Dim y As Integer  'declares y as an integer variable
      Me.SuspendLayout()  'suspends creation of layout
  
      For y = 1 To zonesY 'starts a For loop (1 to zonesY number of loops)
          For x = 1 To zonesX  'starts a For loop (1 to zonesX number of loops)
              Dim zonesize1 As Integer
              Dim zonesize2 As Integer
  
              pbxNewZone = New PictureBox
  
              Dim blockStatus As Integer
              Dim allZones As Integer
              allZones = zonesX * zonesY
              blockStatus = generator.Next(0, allZones)
  
              pbxNewZone.Name = y &amp; ", " &amp; x
              If blockStatus &lt; (allZones / 10) Then
                  pbxNewZone.Tag = True
                  If pbxNewZone.Tag = True Then
                      pbxNewZone.Image = images(8)
                  End If
              Else
                  pbxNewZone.Tag = False
                  If pbxNewZone.Tag = False Then
                      pbxNewZone.Image = images(0)
                  End If
              End If
              pbxNewZone.Height = 16
              pbxNewZone.Width = 16
              pbxNewZone.Tag = 0
              zonesize1 = pbxNewZone.Height 'sets out all of the boxes on the form.
              zonesize2 = pbxNewZone.Width
              pbxNewZone.Left = ((x - 1) * zonesize1 + 15)
              pbxNewZone.Top = ((y - 1) * zonesize2 + 15)
              Me.Controls.Add(pbxNewZone)
              '  Wire this control up to an appropriate event handler
              AddHandler pbxNewZone.Click, AddressOf pbxNewZoneClicked
  
          Next
      Next
      Me.Height = (pbxNewZone.Height * zonesY + 63)  'sets the height of fmmGame
      Me.Width = (pbxNewZone.Width * zonesX + 40)  'sets the width of frmGame
  
  End Sub
  
  Private Sub pbxNewZoneClicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
  
      Dim pb As PictureBox = DirectCast(sender, PictureBox)
      Dim pbTag As Boolean = DirectCast(sender, Boolean)
  
          If pb.Tag = True Then
              pb.Image = images(7) 'Hit Image
          Else
              pb.Image = images(6) 'Clear Image
          End If
  
      MsgBox(pb.Tag)
  
  End Sub
  End Class
  </code></pre>