Reputation: 1082
I'm recreating Minesweeper. All my code is created on runtime, feel free to c&p to help troubleshoot.
I have a loop that creates a grid of pictureboxes (pbxNewZone) with random mines, and sets tag to true if that box is a mine, false if not.
I directcast those pictureboxes (now called "pb") for a click event called "pbxNewZoneClicked", and read the tag. As of now, it showing the mines for testing purposes, and it is showing the hit mine and clear img if i click on a picturebox depending on the condition of the tag.
Now I need to be able to click on an image, and check the 8 imgs around it for mines. All mines are named by their x & y coordinates (literally based on the Integers x and y created on form_load) on the grid and is 1 based, meaning the first mine is named "1, 1" not "0, 0".
So if i click on a pb (renamed directcasted picturebox) named "8, 7", I will Substring out the xValueCheck and yValueCheck variables as "8" and "7", respectively. I then subtract both by one, (to find the box up and to the left), Dim Box1 As String, in this case that would = "7, 6".
Here's the logic I have. Find the pb where name = Box1, and if THAT pb's Tag = True, then counter += 1.
How would I check that pb's Tag WITHIN THE CLICK EVENT, when I'm not clicking on it?
Here's what I got so far:
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 = 9
Dim zonesX As Integer = 9
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("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")
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 & ", " & x
If blockStatus < (allZones / 5) 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(6)
End If
End If
pbxNewZone.Height = 16
pbxNewZone.Width = 16
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)
If active = True Then
Dim pb As PictureBox = DirectCast(sender, PictureBox)
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 xValueCheck As Integer = pb.Name.Substring(0, 1)
MsgBox(xValueCheck) ' To spit out y value from name
Dim yValueCheck As Integer = pb.Name.Substring(3, 1)
MsgBox(yValueCheck) ' To spit out y value from name
Dim Box1 As String = (xValueCheck - 1) & ", " & (yValueCheck - 1)
MsgBox("Box1 = " & Box1, , "Test")
Dim count As Integer = 0
If pb.Tag = True Then
pb.Image = images(7) ' Hit Image
active = False
MsgBox("No Longer Active", , "Test") ' Testing Purposes Only
ElseIf pb.Tag = False Then
'ENTER CODE THAT WILL READ BOXES AROUND IT
'ENTER CODE THAT WILL READ BOXES AROUND IT
'ENTER CODE THAT WILL READ BOXES AROUND IT
'ENTER CODE THAT WILL READ BOXES AROUND IT
'ENTER CODE THAT WILL READ BOXES AROUND IT
pb.Image = images(count) ' Clear Image by default.
End If
End If
End Sub
End Class
Upvotes: 0
Views: 1051
Reputation: 33738
Yipes. Please consider using a 2D array to hold the pictureboxes. Otherwise you will need some (overly complicated) reflection code to go find your controls.
2D array:
Private oGrid(10,10) As PictureBox
Private Sub SetupGrid()
'
' Initialize the grid here
'
' Place the coordinate of the cell in the .Tag property.
'
End Sub
Private Sub GridCellClickHandler(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim tLocation As Point = sender.Tag
'
' Scan around the other 8 cells
' eg. oGrid(tLocation.X - 1, tLocation.Y)
'
End Sub
Of course all this extra data wrangling becomes MUCH easier if you create a new user control to represent the grid cells.
Upvotes: 1