2011-10-27 22 views
0

我正在重新創建掃雷。我的所有代碼都是在運行時創建的,請隨時撥打電話以幫助排除故障。VB.NET如何做一個圖片框的陳述.Name = var

我有一個循環,用隨機地雷創建一個pictureboxes(pbxNewZone)的網格,並且如果這個盒子是我的,那麼將tag設置爲true,否則返回false。

我爲一個名爲「pbxNewZoneClicked」的單擊事件對這些圖片框(現稱爲「pb」)進行直播,並閱讀標籤。到目前爲止,它展示了用於測試目的的礦井,並且它顯示了打擊礦井和清晰img,如果我根據標籤的條件點擊一個圖片框。

現在我需要能夠點擊一張圖片,並檢查它周圍的8個地圖。所有地雷的名字都由它們在網格上的y座標(字面上基於form_load上創建的整數x和y)確定,並且基於1,意味着第一個地雷命名爲「1,1」而不是「0,0」。因此,如果我點擊一個名爲「8,7」的pb(更名爲directcasted picturebox),我將分別將xValueCheck和yValueCheck變量分別作爲「8」和「7」。然後我減去一個,(找到框和向左),Dim Box1 As String,在這種情況下,將=「7,6」。

這是我的邏輯。找到pb,其中name = Box1,如果該pb的Tag = 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 

回答

1

商Yipes。請考慮使用二維數組來保存圖片框。否則,您將需要一些(過於複雜的)反射代碼去查找您的控件。

二維數組:

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 

當然,如果你創建一個新的用戶控件代表網格單元所有這些額外的數據爭論變得更加容易。

+0

如何修改我現有的代碼以使用用戶控件? –