扫雷 VB.NET 问题

发布于 2024-12-10 05:57:38 字数 3408 浏览 4 评论 0原文

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

这一切都是流畅的并且是在运行时构建的。

我正在尝试专门更改单击时的图片框(如果我的= 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

So basically, i have 2 int variables, x and y i am using to create a grid of pictureboxes.

This is all fluid and built on runtime.

I am trying to specifically change the picturebox on click if mine = 2.

I cannot specifically change one, when i click any, it changes all of them.

HELP PLEASE!!

Heres my code:

  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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

芯好空 2024-12-17 05:57:38

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

顺便说一句,您可能需要考虑重构 form_load,以便它调用许多更离散的方法。

You're adding the same handler to all the PictureBoxes but not doing anything to the specific PictureBox that was clicked (the Sender parameter). You can use the Name or Tag properties to work out what to do with the click. You might want to extend the standard PictureBox to include extra parameters that will make this easier - x and y properties for example.

As an aside you might want to consider re-factoring form_load so that it calls a number of more discrete methods.

云裳 2024-12-17 05:57:38

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

Dim pbx as PictureBox = DirectCast(sender,PictureBox)

...将为您提供对单击的 PictureBox 的引用(如 pbx) - 然后您可以执行以下操作你需要用它做什么。

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

In a Click event, sender will be the clicked object so...

Dim pbx as PictureBox = DirectCast(sender,PictureBox)

...will give you a reference (as pbx) to the clicked PictureBox - then you can do what you need to do with it.

For ease, you may want to check the state of a given PictureBox by checking pbx.Image - as you do not have any custom attributes of the PictureBox.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文