Rick Bross
Rick Bross

Reputation: 1082

Targeting PictureBox in 2D array

I have a PictureBox array that creates a 10,10 grid of pictures, how can I taget one of those pictureboxes?

Public Class Form1
Inherits System.Windows.Forms.Form
Dim active As Boolean = True
Dim images(8) As Image 'declares image array

Dim zonesY As Integer = 10
Dim zonesX As Integer = 100

Dim Guy As Object

Dim generator As New Random
' Dim x As Integer  'declares x as an integer variable
' Dim y As Integer  'declares y as an integer variable
Dim oGrid(zonesX, zonesY) As PictureBox

Private Sub SetupGrid()
    images(0) = Image.FromFile("clear.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("blank.png")
    images(7) = Image.FromFile("hit.png")
    images(8) = Image.FromFile("mine.png")

    For y As Integer = 1 To zonesY 'starts a For loop (1 to zonesY number of loops)
        For x As Integer = 1 To zonesX  'starts a For loop (1 to zonesX number of loops)
            Dim zonesize1 As Integer
            Dim zonesize2 As Integer

            Dim blockStatus As Integer
            Dim allZones As Integer
            allZones = zonesX * zonesY
            blockStatus = generator.Next(0, allZones)

            oGrid(x, y).Name = x & ", " & y

            MsgBox(oGrid(x, y).Name)

            If blockStatus < (allZones / 5) Then
                oGrid(x, y).Tag = True
                If oGrid(x, y).Tag = True Then
                    oGrid(x, y).Image = images(8)
                End If
            Else
                oGrid(x, y).Tag = False
                If oGrid(x, y).Tag = False Then
                    oGrid(x, y).Image = images(6)
                End If
            End If
            oGrid(x, y).Height = 16
            oGrid(x, y).Width = 16
            zonesize1 = oGrid(x, y).Height 'sets out all of the boxes on the form.
            zonesize2 = oGrid(x, y).Width
            oGrid(x, y).Left = ((x - 1) * zonesize1 + 15)
            oGrid(x, y).Top = ((y - 1) * zonesize2 + 15)
            Me.Controls.Add(oGrid(x, y))
            '  Wire this control up to an appropriate event handler
            AddHandler oGrid(x, y).Click, AddressOf pbxNewZoneClicked

        Next
    Next

    Me.Height = 500   'sets the height of fmmGame
    Me.Width = 500  'sets the width of frmGame

End Sub



Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    SetupGrid()

End Sub

Private Sub CheckHits()
    Dim FicX1 As Integer = zonesX - 1
    Dim FicY1 As Integer = zonesY - 1

    Dim counter As Integer = 0

End Sub

Private Sub pbxNewZoneClicked(ByVal sender As System.Object, ByVal e As System.EventArgs)

    If active = True Then
        Dim pb As PictureBox = DirectCast(sender, PictureBox)

        Dim splitName() As String = pb.Name.Split(",")
        Dim x As Integer = Convert.ToInt32(splitName(0))
        Dim y As Integer = Convert.ToInt32(splitName(1))

        Dim Status As String = "Clear" ' Status - Testing Purposes Only
        If pb.Tag = True Then ' Status - Testing Purposes Only
            Status = "Mine" ' Status - Testing Purposes Only
        End If
        ' MsgBox(pb.Name & vbCrLf & "Status: " & Status, , "Test") ' Post Statistics of box.


        Dim counter As Integer = 0


        If oGrid(x, y).Tag = True Then
            pb.Image = images(7) ' Hit Image
            active = False
            ' MsgBox("No Longer Active", , "Test") ' Testing Purposes Only
        ElseIf oGrid(x, y).Tag = False Then
            pb.Image = images(counter) ' Clear Image by default.
        End If

    End If
End Sub

End Class

I'm trying to check if the picturebox up one, and left one's tag = True to += 1 the counter.

Upvotes: 0

Views: 1143

Answers (2)

LarsTech
LarsTech

Reputation: 81620

Comment out these lines:

'Dim x As Integer  'declares x as an integer variable
'Dim y As Integer  'declares y as an integer variable

Change this line:

Dim oGrid(zonesX, zonesY) As PictureBox

Change your loops to this:

For y As Integer = 1 To zonesY 'starts a For loop (1 to zonesY number of loops)
  For x As Integer = 1 To zonesX  'starts a For loop (1 to zonesX number of loops)
    '// blah-blah
  Next
Next

Add this to your pbxNewZoneClicked:

Dim splitName() As String = pb.Name.Split(",")
Dim x As Integer = Convert.ToInt32(splitName(0))
Dim y As Integer = Convert.ToInt32(splitName(1))

This "should" get your program running.

Is this the best way to do this? Probably not, but that's a much longer conversation.

Upvotes: 1

LarsTech
LarsTech

Reputation: 81620

You haven't given much to work on. I looked at your other questions to try to piece it together.

I "think" you want to do something like this:

Private Sub CheckHits(ByVal column As Integer, ByVal row As Integer)

  'Above Left:
  If column > 0 And row > 0 Then
    If oGrid(column - 1, row - 1).Tag Then
      counter += 1
    End If
  End If

  'Above:
  If row > 0 Then
    If oGrid(column, row - 1).Tag Then
      counter += 1
    End If
  End If

  '// continue going around the cell
End Sub

In your posted code you are referencing a 2nd 2-dim array Picturebox(,) where it looks like you already have everything in the oGrid(,) array. I'm just using the oGrid array.

Also, you have to check the boundaries. If you are checking the (0, 0) square, you obviously have to ignore the left and above coordinate since they don't exist in the grid.

Upvotes: 1

Related Questions