如何在 VB 中请求多个 Picturebox-Clickevents
How do I ask for multiple Picturebox-Clickevents in VB
我有一个 10x10 的图片框数组,用户需要单击这些图片框,但我显然懒得写 100 个 "Picturebox.Onclick"-段落,数组中的每个图片框一个。我能做什么?
我知道的唯一方法是为每个图片框设置一个段落,我已经为多达 8 个图片框这样做了。但是对于 100?啊哈。
我知道必须有更简单的方法,但我想不出来。
您可以将图片框数组转换为列表,然后将处理程序添加到列表中项目的鼠标单击事件。像这样
Dim piclist As New List (Of PictureBox)
'Convert 2d array to 1d array and store in a List
'***********************
' Replace picarr with the name of your picture box array
For x As Integer = 0 To 9
For y As Integer = 0 To 9
piclist.Items.Add(picarr(x,y)
Next
Next
'*************************
For Each item As PictureBox in piclist
AddHandler item.Click, AddressOf picclick
Next
Public Sub picclick (sender As Object, e As EventArgs)
'Add actions here
End Sub
我有一个 10x10 的图片框数组,用户需要单击这些图片框,但我显然懒得写 100 个 "Picturebox.Onclick"-段落,数组中的每个图片框一个。我能做什么?
我知道的唯一方法是为每个图片框设置一个段落,我已经为多达 8 个图片框这样做了。但是对于 100?啊哈。 我知道必须有更简单的方法,但我想不出来。
您可以将图片框数组转换为列表,然后将处理程序添加到列表中项目的鼠标单击事件。像这样
Dim piclist As New List (Of PictureBox)
'Convert 2d array to 1d array and store in a List
'***********************
' Replace picarr with the name of your picture box array
For x As Integer = 0 To 9
For y As Integer = 0 To 9
piclist.Items.Add(picarr(x,y)
Next
Next
'*************************
For Each item As PictureBox in piclist
AddHandler item.Click, AddressOf picclick
Next
Public Sub picclick (sender As Object, e As EventArgs)
'Add actions here
End Sub