宣告Picturebox控键数组后如何去让这些控键数组都有触发事件的功能小弟知道如下面的代码 是能正常的去触发
宣告Picturebox控键数组后如何去让这些控键数组都有触发事件的功能
小弟知道如下面的代码 是能正常的去触发这个对象产生事件
Dim WithEvents b As New Button
但是如果变成是用如下面的代码:
Private pic(11) As PictureBox
这样的状况下又不能加入WithEvents
Private WithEvents pic(11) As PictureBox
编译程序会回报错误讯息 报说数组下不能加入WithEvents
请问各位大哥这边有什么变通的技巧吗?
[解决办法]Public Class Form1
Private pic(9) As PictureBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
tk()
End Sub
Sub tk()
For Each ppic In pic
ppic = New PictureBox
With ppic
.Top = Me.PictureBox1.Top
.Left = 40 + i * 50
.BackColor = Me.PictureBox1.BackColor
.Size = PictureBox1.Size
.Name = "ppic" & i
.Visible = True
End With
Me.GroupBox1.Controls.Add(ppic) '最容易疏忽的一行
i += 1
Next
i = 1
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
i = 1
For Each picture As PictureBox In Me.GroupBox1.Controls
If TypeOf (picture) Is PictureBox Then
AddHandler picture.Click, AddressOf PictureBox1_Click
End If
Next
MessageBox.Show("綁定控制項")
End Sub
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
MessageBox.Show(CType(sender, PictureBox).Name)
End Sub
Dim i = 1
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
For Each c In me.Controls '遍历控件
if c.Name.StartWidth("ppic") then '加了一个判断
dim ppic as PictureBox = ctype(c,PictureBox)
With ppic
.Top = Me.PictureBox1.Top
.Left = 40 + i * 50
.BackColor = Color.Red
.Size = PictureBox1.Size
.Name = "ppic" & i
.Visible = True
End With
i += 1
end if
Next
End Sub
End Class
[解决办法]
是 StartsWith