VB.NET 如何对图片框执行语句 where .Name = var

发布于 2024-12-12 00:22:40 字数 4710 浏览 0 评论 0原文

我正在重新制作扫雷。我的所有代码都是在运行时创建的,请随时 c&p 来帮助排除故障。

我有一个循环,它创建带有随机地雷的图片框网格(pbxNewZone),如果该框是地雷,则将标记设置为 true,如果不是,则将标记设置为 false。

我将这些图片框(现在称为“pb”)直接投射到名为“pbxNewZoneClicked”的点击事件,并读取标签。到目前为止,它显示了用于测试目的的地雷,并且如果我根据标签的条件单击图片框,它会显示命中的地雷和清除图像。

现在我需要能够点击一个图像,并检查它周围的 8 个图像是否有地雷。所有地雷均以其 x 和 来命名。网格上的 y 坐标(字面上基于在 form_load 上创建的整数 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

I'm recreating Minesweeper. All my code is created on runtime, feel free to c&p to help troubleshoot.

I have a loop that creates a grid of pictureboxes (pbxNewZone) with random mines, and sets tag to true if that box is a mine, false if not.

I directcast those pictureboxes (now called "pb") for a click event called "pbxNewZoneClicked", and read the tag. As of now, it showing the mines for testing purposes, and it is showing the hit mine and clear img if i click on a picturebox depending on the condition of the tag.

Now I need to be able to click on an image, and check the 8 imgs around it for mines. All mines are named by their x & y coordinates (literally based on the Integers x and y created on form_load) on the grid and is 1 based, meaning the first mine is named "1, 1" not "0, 0".

So if i click on a pb (renamed directcasted picturebox) named "8, 7", I will Substring out the xValueCheck and yValueCheck variables as "8" and "7", respectively. I then subtract both by one, (to find the box up and to the left), Dim Box1 As String, in this case that would = "7, 6".

Here's the logic I have. Find the pb where name = Box1, and if THAT pb's Tag = True, then counter += 1.

How would I check that pb's Tag WITHIN THE CLICK EVENT, when I'm not clicking on it?

Here's what I got so far:

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

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

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

发布评论

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

评论(1

緦唸λ蓇 2024-12-19 00:22:40

伊佩斯。请考虑使用二维数组来保存图片框。否则,您将需要一些(过于复杂的)反射代码来查找控件。

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

当然,如果您创建一个新的用户控件来表示网格单元,所有这些额外的数据整理都会变得容易得多。

Yipes. Please consider using a 2D array to hold the pictureboxes. Otherwise you will need some (overly complicated) reflection code to go find your controls.

2D array:

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

Of course all this extra data wrangling becomes MUCH easier if you create a new user control to represent the grid cells.

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