[英]VB.NET How to do statement to a picturebox where .Name = var
I'm recreating Minesweeper. 我正在重新创建Minesweeper。 All my code is created on runtime, feel free to c&p to help troubleshoot.
我所有的代码都是在运行时创建的,请随时通过c&p帮助进行故障排除。
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. 我有一个循环,该循环创建带有随机地雷的图片框(pbxNewZone)网格,如果该框是地雷,则将tag设置为true,否则将false设置为false。
I directcast those pictureboxes (now called "pb") for a click event called "pbxNewZoneClicked", and read the tag. 我直接为称为“ pbxNewZoneClicked”的单击事件广播了这些图片框(现在称为“ pb”),并读取了标记。 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.
到目前为止,它显示用于测试的地雷,并且如果我根据标签的条件单击图片框,则会显示命中的地雷并清除img。
Now I need to be able to click on an image, and check the 8 imgs around it for mines. 现在,我需要能够单击图像,并检查周围的8张img是否存在地雷。 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".
所有地雷均通过网格上的x和y坐标(从字面上基于在form_load上创建的Integers x和y的坐标)来命名,并且基于1,这意味着第一个地雷的名称为“ 1,1”而不是“ 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. 因此,如果我单击名为“ 8,7”的pb(重命名为直接广播的图片框),则将xValueCheck和yValueCheck变量分别分列为“ 8”和“ 7”。 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".
然后,我将两者都减去一个(在框的左上角),Dim Box1 As String,在这种情况下,该值=“ 7,6”。
Here's the logic I have. 这是我的逻辑。 Find the pb where name = Box1, and if THAT pb's Tag = True, then counter += 1.
查找名称为Box1的pb,如果该pb的标签= True,则计数器+ = 1。
How would I check that pb's Tag WITHIN THE CLICK EVENT, when I'm not clicking on it? 当我不单击它时,如何查看pb的标签?
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
Yipes. 是的。 Please consider using a 2D array to hold the pictureboxes.
请考虑使用2D阵列保存图片框。 Otherwise you will need some (overly complicated) reflection code to go find your controls.
否则,您将需要一些(过于复杂的)反射代码来查找控件。
2D array: 2D阵列:
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. 当然,如果您创建一个新的用户控件来表示网格单元,那么所有这些额外的数据整理将变得更加容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.