所以基本上,我已经成功地随机化了一个网格中的某些图片框以包含地雷,并显示我的,并且出于测试目的,这些地雷目前正在展示。您认为我需要做什么才能说:
如果,你单击此框并且我的= 1(有一个我的),那么你输了。
否则,继续前进。
很简单,但我想将它应用于所有盒子,无论网格有多大。 (ZonesX * Zones 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
答案 0 :(得分:2)
快速执行此操作的方法是使用Tag
的{{1}}元素来跟踪是否有我的(或任何其他条件)。
PictureBox
然后,通过将pbxnewzone.Tag = 1 'can assign tags to all of your pictureboxes in a loop or at random if need be
方法的Tag
参数转换回sender
来访问pbxNewZoneClicked
属性:
PictureBox
答案 1 :(得分:1)
除非我遗漏了某些内容,请设置minePictureBox1.Tag = true,然后在click事件中进行检查。或者您可以将其设置为1或其他。标签是一个对象。
这就是你要找的东西吗?只是一种了解它是否是我的方式的方法?
答案 2 :(得分:1)
我认为整个网格的一个面板比每个单元格的图片框更容易管理,资源更少。
但是对于您的点击问题:
Private Sub pbxNewZoneClicked(ByVal sender As Object, ByVal e As EventArgs)
With DirectCast(sender, PictureBox)
MsgBox(.Name)
End With
End Sub