2

所以基本上,我已经成功地将网格中的某些图片框随机化以包含地雷,并显示该地雷,并且出于测试目的,这些地雷当前正在显示。你认为我需要做什么才能说:

如果您单击此框并且 mine = 1(有地雷),那么您就输了。
否则,继续。

很简单,但我想把它应用到所有盒子上,不管网格有多大。(X 区 * Y 区)

我得到的最远的是当单击其中任何一个时将弹出一个 MsgBox() 。

这都是在运行时创建的。这是我的代码。

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
4

3 回答 3

2

一种快速的方法是使用 的Tag元素PictureBox来跟踪是否有地雷(或任何其他条件)。

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

然后,通过将方法的参数转换回 a 来访问该Tag属性:senderpbxNewZoneClickedPictureBox

Dim pb As PictureBox = DirectCast(sender, PictureBox)
If (pb.Tag = 1) Then
    'change game accordingly
End If
于 2011-10-18T15:50:58.880 回答
1

除非我遗漏了什么,否则设置 minePictureBox1.Tag = true 然后在点击事件上检查它。或者您可以将其设置为 1 或其他任何值。标签是一个对象。

那是你要找的吗?只是一种知道它是否是矿井的方法吗?

于 2011-10-18T15:50:19.967 回答
1

我认为整个网格的一个面板会比每个单元格的图片框更易于管理且资源消耗更少。

但是对于您的点击问题:

Private Sub pbxNewZoneClicked(ByVal sender As Object, ByVal e As EventArgs)
  With DirectCast(sender, PictureBox)
    MsgBox(.Name)
  End With
End Sub
于 2011-10-18T15:52:55.177 回答