在2D数组中定位PictureBox

时间:2011-11-08 16:33:57

标签: vb.net multidimensional-array

我有一个PictureBox数组,可以创建一个10,10网格的图片,我怎么能看到其中一个图片框呢?

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 = 10
Dim zonesX As Integer = 100

Dim Guy As Object

Dim generator As New Random
' Dim x As Integer  'declares x as an integer variable
' Dim y As Integer  'declares y as an integer variable
Dim oGrid(zonesX, zonesY) As PictureBox

Private Sub SetupGrid()
    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")

    For y As Integer = 1 To zonesY 'starts a For loop (1 to zonesY number of loops)
        For x As Integer = 1 To zonesX  'starts a For loop (1 to zonesX number of loops)
            Dim zonesize1 As Integer
            Dim zonesize2 As Integer

            Dim blockStatus As Integer
            Dim allZones As Integer
            allZones = zonesX * zonesY
            blockStatus = generator.Next(0, allZones)

            oGrid(x, y).Name = x & ", " & y

            MsgBox(oGrid(x, y).Name)

            If blockStatus < (allZones / 5) Then
                oGrid(x, y).Tag = True
                If oGrid(x, y).Tag = True Then
                    oGrid(x, y).Image = images(8)
                End If
            Else
                oGrid(x, y).Tag = False
                If oGrid(x, y).Tag = False Then
                    oGrid(x, y).Image = images(6)
                End If
            End If
            oGrid(x, y).Height = 16
            oGrid(x, y).Width = 16
            zonesize1 = oGrid(x, y).Height 'sets out all of the boxes on the form.
            zonesize2 = oGrid(x, y).Width
            oGrid(x, y).Left = ((x - 1) * zonesize1 + 15)
            oGrid(x, y).Top = ((y - 1) * zonesize2 + 15)
            Me.Controls.Add(oGrid(x, y))
            '  Wire this control up to an appropriate event handler
            AddHandler oGrid(x, y).Click, AddressOf pbxNewZoneClicked

        Next
    Next

    Me.Height = 500   'sets the height of fmmGame
    Me.Width = 500  'sets the width of frmGame

End Sub



Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    SetupGrid()

End Sub

Private Sub CheckHits()
    Dim FicX1 As Integer = zonesX - 1
    Dim FicY1 As Integer = zonesY - 1

    Dim counter As Integer = 0

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 splitName() As String = pb.Name.Split(",")
        Dim x As Integer = Convert.ToInt32(splitName(0))
        Dim y As Integer = Convert.ToInt32(splitName(1))

        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 counter As Integer = 0


        If oGrid(x, y).Tag = True Then
            pb.Image = images(7) ' Hit Image
            active = False
            ' MsgBox("No Longer Active", , "Test") ' Testing Purposes Only
        ElseIf oGrid(x, y).Tag = False Then
            pb.Image = images(counter) ' Clear Image by default.
        End If

    End If
End Sub

End Class

我试图检查图片框是否一个,并将一个标签= True给+ = 1计数器。

HELP PLEASE !!!!

2 个答案:

答案 0 :(得分:1)

你没有做太多工作。我看了你的其他问题,试着把它拼凑起来。

我“想”你想做这样的事情:

Private Sub CheckHits(ByVal column As Integer, ByVal row As Integer)

  'Above Left:
  If column > 0 And row > 0 Then
    If oGrid(column - 1, row - 1).Tag Then
      counter += 1
    End If
  End If

  'Above:
  If row > 0 Then
    If oGrid(column, row - 1).Tag Then
      counter += 1
    End If
  End If

  '// continue going around the cell
End Sub

在您发布的代码中,您引用了第二个2-dim数组Picturebox(,),看起来您已经拥有oGrid(,)数组中的所有内容。我只是使用oGrid数组。

另外,你必须检查边界。如果你正在检查(0,0)方格,你显然必须忽略左边和上面的坐标,因为它们在网格中不存在。

答案 1 :(得分:1)

注释掉这些内容:

'Dim x As Integer  'declares x as an integer variable
'Dim y As Integer  'declares y as an integer variable

更改此行:

Dim oGrid(zonesX, zonesY) As PictureBox

将循环更改为:

For y As Integer = 1 To zonesY 'starts a For loop (1 to zonesY number of loops)
  For x As Integer = 1 To zonesX  'starts a For loop (1 to zonesX number of loops)
    '// blah-blah
  Next
Next

将此添加到您的pbxNewZoneClicked:

Dim splitName() As String = pb.Name.Split(",")
Dim x As Integer = Convert.ToInt32(splitName(0))
Dim y As Integer = Convert.ToInt32(splitName(1))

这应该“让你的程序运行。”

这是最好的方法吗?可能不是,但这是一个更长的对话。