[英]VB.NET How to do statement to a picturebox where .Name = var
我正在重新創建Minesweeper。 我所有的代碼都是在運行時創建的,請隨時通過c&p幫助進行故障排除。
我有一個循環,該循環創建帶有隨機地雷的圖片框(pbxNewZone)網格,如果該框是地雷,則將tag設置為true,否則將false設置為false。
我直接為稱為“ pbxNewZoneClicked”的單擊事件廣播了這些圖片框(現在稱為“ pb”),並讀取了標記。 到目前為止,它顯示用於測試的地雷,並且如果我根據標簽的條件單擊圖片框,則會顯示命中的地雷並清除img。
現在,我需要能夠單擊圖像,並檢查周圍的8張img是否存在地雷。 所有地雷均通過網格上的x和y坐標(從字面上基於在form_load上創建的Integers x和y的坐標)來命名,並且基於1,這意味着第一個地雷的名稱為“ 1,1”而不是“ 0,0”。
因此,如果我單擊名為“ 8,7”的pb(重命名為直接廣播的圖片框),則將xValueCheck和yValueCheck變量分別分列為“ 8”和“ 7”。 然后,我將兩者都減去一個(在框的左上角),Dim Box1 As String,在這種情況下,該值=“ 7,6”。
這是我的邏輯。 查找名稱為Box1的pb,如果該pb的標簽= True,則計數器+ = 1。
當我不單擊它時,如何查看pb的標簽?
這是到目前為止我得到的:
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
是的。 請考慮使用2D陣列保存圖片框。 否則,您將需要一些(過於復雜的)反射代碼來查找控件。
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
當然,如果您創建一個新的用戶控件來表示網格單元,那么所有這些額外的數據整理將變得更加容易。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.