我如何定位在运行时创建的图片框之一?网络

发布于 2024-12-10 14:06:51 字数 3248 浏览 0 评论 0原文

所以基本上,我已经成功地随机化网格中的某些图片框来包含地雷,并显示我的地雷,并且出于测试目的,这些地雷当前正在显示。你认为我需要做什么才能说:

如果你点击这个框并且我的 = 1(有一个地雷),那么你就输了。
否则,继续。

很简单,但我想将其应用于所有盒子,无论网格有多大。 (ZonesX * Zones Y)

我得到的最远的是当单击其中任何一个时将弹出一个 MsgBox() 。

这都是在运行时创建的。这是我的代码。

Public Class Form1
Inherits System.Windows.Forms.Form

Dim images(8) As Image 'declares image array

Dim zonesY As Integer = 10
Dim zonesX As Integer = 10

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

            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 / 10) 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(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

End Sub

Private Sub pbxNewZoneClicked(ByVal sender As System.Object, ByVal e As System.EventArgs)

    Dim pb As PictureBox = DirectCast(sender, PictureBox)
    Dim pbTag As Boolean = DirectCast(sender, Boolean)

        If pb.Tag = True Then
            pb.Image = images(7) 'Hit Image
        Else
            pb.Image = images(6) 'Clear Image
        End If

    MsgBox(pb.Tag)

End Sub
End Class

So basically, i have successfully randomized certain pictureboxes in a grid to contain mines, and show that mine, and for testing purposes, those mines are currently showing. What do you think I need to do to be able to say:

If, you click this box and mine = 1 (there's a mine), then you lose.
Else, keep going.

simple enough, but i want to apply this to all boxes, no matter how big the grid is. (ZonesX * Zones Y)

The furthest I have gotten is being about to pop up a MsgBox() when anyone of them is clicked.

This is all created on runtime. Heres my code.

Public Class Form1
Inherits System.Windows.Forms.Form

Dim images(8) As Image 'declares image array

Dim zonesY As Integer = 10
Dim zonesX As Integer = 10

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

            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 / 10) 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(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

End Sub

Private Sub pbxNewZoneClicked(ByVal sender As System.Object, ByVal e As System.EventArgs)

    Dim pb As PictureBox = DirectCast(sender, PictureBox)
    Dim pbTag As Boolean = DirectCast(sender, Boolean)

        If pb.Tag = True Then
            pb.Image = images(7) 'Hit Image
        Else
            pb.Image = images(6) 'Clear Image
        End If

    MsgBox(pb.Tag)

End Sub
End Class

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

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

发布评论

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

评论(3

新人笑 2024-12-17 14:06:51

一种快速的方法是使用 PictureBoxTag 元素来跟踪是否有地雷(或任何其他条件)。

pbxnewzone.Tag = 1   'can assign tags to all of your pictureboxes in a loop or at random if need be

然后,通过将 pbxNewZoneClicked 方法的 sender 参数强制转换回 PictureBox 来访问 Tag 属性:

Dim pb As PictureBox = DirectCast(sender, PictureBox)
If (pb.Tag = 1) Then
    'change game accordingly
End If

A quick way to do it would be to use the Tag element of the PictureBox to keep track of whether there is a mine or not (or any of your other conditions).

pbxnewzone.Tag = 1   'can assign tags to all of your pictureboxes in a loop or at random if need be

Then, access the Tag property by casting the sender argument of your pbxNewZoneClicked method back to a PictureBox:

Dim pb As PictureBox = DirectCast(sender, PictureBox)
If (pb.Tag = 1) Then
    'change game accordingly
End If
仙女 2024-12-17 14:06:51

除非我遗漏了某些内容,否则请设置minePictureBox1.Tag = true,然后在单击事件上检查它。或者你可以将其设置为 1 或其他值。标签是一个对象。

这就是您要找的吗?只是一个方法来知道它是否是一个地雷?

Unless I'm missing something, set minePictureBox1.Tag = true then check it on the click event. Or you could set it to 1 or whatever. Tag is an object.

Is that what you're looking for? Just a way to know if it's a mine or not?

離殇 2024-12-17 14:06:51

我认为整个网格的一个面板比每个单元格的图片框更容易管理,并且占用的资源更少。

但对于您的点击问题:

Private Sub pbxNewZoneClicked(ByVal sender As Object, ByVal e As EventArgs)
  With DirectCast(sender, PictureBox)
    MsgBox(.Name)
  End With
End Sub

I would think one panel for the entire grid would be easier to manage and less resource hungry then a picturebox for each and every cell.

But for your click issue:

Private Sub pbxNewZoneClicked(ByVal sender As Object, ByVal e As EventArgs)
  With DirectCast(sender, PictureBox)
    MsgBox(.Name)
  End With
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文