扫雷VB.NET问题

时间:2011-10-17 15:37:55

标签: vb.net for-loop global-variables minesweeper

所以基本上,我有2个int变量,x和y我用来创建一个图片框网格。

这是流畅的,并且是在运行时构建的。

如果我的= 2,我试图专门更改图片框。

我无法专门更改一个,当我点击任何一个时,它会改变所有这些。

请帮助!!

继承我的代码:

  Public Class Form1
    Inherits System.Windows.Forms.Form

    Dim images(8) As Image 'declares image array

    Dim zonesY As Integer = 50
    Dim zonesX As Integer = 50

    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
                Dim mine As Integer

                pbxNewZone = New PictureBox

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

                pbxNewZone.Name = (zonesX * (y - 1)) + x
                If blockStatus < (allZones / 10) Then
                    mine = 1
                    If mine = 1 Then
                        pbxNewZone.Image = images(8)
                    End If
                Else
                    mine = 2
                    If mine = 2 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

        checkBlank()

    End Sub

    Public Sub checkBlank()

    End Sub

    Private Sub pbxNewZoneClicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ReDim x
        Do While y = 1 'starts a For loop (1 to zonesY number of loops)
            Do While x = 1  'starts a For loop (1 to zonesX number of loops)
                MsgBox("you have clicked " & x & ", " & y)
            Loop
        Loop
    End Sub

End Class

2 个答案:

答案 0 :(得分:4)

您正在为所有PictureBox添加相同的处理程序,但没有对单击的特定PictureBox(Sender参数)执行任何操作。您可以使用“名称”或“标记”属性来确定如何处理单击。您可能希望扩展标准PictureBox以包含额外的参数,这些参数将使这更容易 - 例如x和y属性。

另外,您可能需要考虑重新分解form_load,以便调用一些更离散的方法。

答案 1 :(得分:1)

在点击事件中,sender将是点击的对象,因此......

Dim pbx as PictureBox = DirectCast(sender,PictureBox)

...将为您点击的PictureBox提供参考(如pbx) - 然后您可以执行您需要的操作。

为方便起见,您可能需要通过检查pbx.Image来检查给定PictureBox的状态,因为您没有PictureBox的任何自定义属性。