我已经设置了一个使用自定义类生成72个图片框的网格。我现在需要检查用户是否点击了这些对象中的任何一个。
Picturebox类:
Public Class Tile
Inherits PictureBox
Public Side As Char
Public Spawnable As Boolean
Public Inhabiter As Integer = 000
Public Sub New(ByVal LeftValue As Integer, ByVal TopValue As Integer)
With Me
.Size = New Size(70, 70)
.BringToFront()
.Top = TopValue
.Left = LeftValue
End With
End Sub
End Class
以下是网格制作
Sub SetBoard()
Dim HorizontalCounter, VerticalCounter, TopValue, LeftValue As Integer
TopValue = 90
LeftValue = 275
Do Until VerticalCounter = 6
Do Until HorizontalCounter = 12
Dim BoardTile As New Tile(LeftValue, TopValue)
Tiles.Add(BoardTile)
Controls.Add(BoardTile)
Map.Controls.Add(BoardTile)
LeftValue += 80
HorizontalCounter += 1
'If LeftValue < 800 Then BoardTile.Side = "R" Else BoardTile.Side = "B"
'If LeftValue < 550 Or LeftValue > 1000 Then BoardTile.Spawnable = True Else BoardTile.Spawnable = False
BoardTile.BackColor = Color.Transparent
'If BoardTile.Side = "R" Then BoardTile.BackColor = Color.Red Else BoardTile.BackColor = Color.Blue
'If BoardTile.Spawnable = True Then BoardTile.BackColor = Color.Green
Loop
LeftValue = 275
HorizontalCounter = 0
TopValue += 80
VerticalCounter += 1
Loop
End Sub
答案 0 :(得分:1)
创建将处理click事件的方法:
Private Sub BoardTile_Click(sender As Object, e As EventArgs)
Dim t As Tile = CType(sender, Tile)
MessageBox.Show(t.Name)
End Sub
然后在创建控件时,连接事件:
Dim BoardTile As New Tile(LeftValue, TopValue)
BoardTile.Name = LeftValue.ToString() & " - " & TopValue.ToString()
AddHandler BoardTile.Click, AddressOf BoardTile_Click