2

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:

If, you click this box and mine = 1 (there's a mine), then you lose.
Else, keep going.

simple enough, but i want to apply this to all boxes, no matter how big the grid is. (ZonesX * Zones Y)

The furthest I have gotten is being about to pop up a MsgBox() when anyone of them is clicked.

This is all created on runtime. Heres my 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 & ", " & x
            If blockStatus < (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
Rick Bross
  • 1,060
  • 3
  • 16
  • 39

3 Answers3

2

A quick way to do it would be to use the Tag element of the PictureBox to keep track of whether there is a mine or not (or any of your other conditions).

pbxnewzone.Tag = 1   'can assign tags to all of your pictureboxes in a loop or at random if need be

Then, access the Tag property by casting the sender argument of your pbxNewZoneClicked method back to a PictureBox:

Dim pb As PictureBox = DirectCast(sender, PictureBox)
If (pb.Tag = 1) Then
    'change game accordingly
End If
jonsca
  • 10,218
  • 26
  • 54
  • 62
  • Got it. Now i have it checking for tag = 1 or 0, and it assigns the mine image randomly, but lower in the code, as you suggested I have i have it checking for tag on the click of a box. When I say if pb.Tag = 1 it does nothing, and when i have ElseIf pb.Tag = 2 it does nothing (same for = True/False). I only get a result when i use Else, even at that i can only change it to one image, and it does not recognize a mine or a clear. So i have concluded that its not pulling the .Tag element from the loop. How to I spit the .Tag = 1/2 (True/False) out of the loop, and into the Click event for pb? – Rick Bross Oct 25 '11 at 14:54
  • Loop: For y = 1 To zonesY 'starts a For loop and creates grid (1 to zonesY number of loops) For x = 1 To zonesX 'starts a For loop (1 to zonesX number of loops) – Rick Bross Oct 25 '11 at 15:05
  • I MsgBox(pb.Tag)'d it, its always returning 0. As I see it, DirectCast works as a portal from **Public Sub Form1_Load** to **Private Sub pbxNewZoneClicked**. Do I need to somehow pass the pbxNewZone.Tag through that door/portal? – Rick Bross Oct 25 '11 at 15:09
  • There's no portal, it's event driven, so every time the box is clicked it raised that `pbxNewZoneClicked` event. Whichever box is clicked is implicitly sent into that method as `sender`, but you must cast it back to whichever type of object that it is to access the members and methods. Assign each box a tag in a loop in the `Load` method. – jonsca Oct 25 '11 at 15:13
  • I do. And based on whether its True or False (from Random), it assigns a mine.png to the Picturebox. If i MsgBox(pbxNewZone.Tag) it spits out True or False. But if i MsgBox(pb.Tag) after the DirectCast, it always sends 0. – Rick Bross Oct 25 '11 at 15:19
  • feel free to copy and paste my code into VB 2010, its all created on runtime, so it would be easy for you to debug. Code is updated. – Rick Bross Oct 25 '11 at 15:22
  • HAHAHAHAHAH, i set it back to .Tag = 0 everytime! – Rick Bross Oct 25 '11 at 15:27
1

Unless I'm missing something, set minePictureBox1.Tag = true then check it on the click event. Or you could set it to 1 or whatever. Tag is an object.

Is that what you're looking for? Just a way to know if it's a mine or not?

Yatrix
  • 13,361
  • 16
  • 48
  • 78
1

I would think one panel for the entire grid would be easier to manage and less resource hungry then a picturebox for each and every cell.

But for your click issue:

Private Sub pbxNewZoneClicked(ByVal sender As Object, ByVal e As EventArgs)
  With DirectCast(sender, PictureBox)
    MsgBox(.Name)
  End With
End Sub
LarsTech
  • 80,625
  • 14
  • 153
  • 225