2

したがって、基本的に、グリッド内の特定のピクチャボックスをランダム化して地雷を含めることに成功し、その地雷を表示し、テスト目的で、それらの地雷が現在表示されています。次のように言えるようになるには、どうすればよいと思いますか。

このボックスをクリックして mine = 1 (地雷がある) の場合、負けとなります。
そうでなければ、続けてください。

単純ですが、グリッドの大きさに関係なく、これをすべてのボックスに適用したいと思います。(ゾーン X * ゾーン 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
4

3 に答える 3

2

これを行う簡単な方法は、 のTag要素を使用しPictureBoxて、地雷があるかどうか (またはその他の条件) を追跡することです。

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

次に、メソッドの引数をTagキャストしてプロパティにアクセスします。senderpbxNewZoneClickedPictureBox

Dim pb As PictureBox = DirectCast(sender, PictureBox)
If (pb.Tag = 1) Then
    'change game accordingly
End If
于 2011-10-18T15:50:58.880 に答える
1

何か不足していない限り、 minePictureBox1.Tag = true を設定してから、クリックイベントで確認してください。または、1 などに設定することもできます。タグはオブジェクトです。

それはあなたが探しているものですか?それが鉱山かどうかを知る方法は?

于 2011-10-18T15:50:19.967 に答える
1

グリッド全体に 1 つのパネルを使用すると、すべてのセルのピクチャボックスよりも管理が容易になり、リソースの消費が少なくなると思います。

しかし、クリックの問題については:

Private Sub pbxNewZoneClicked(ByVal sender As Object, ByVal e As EventArgs)
  With DirectCast(sender, PictureBox)
    MsgBox(.Name)
  End With
End Sub
于 2011-10-18T15:52:55.177 に答える