E生相爱工作室

18778998308服务热线

好管家网络

VB.NET 中动态添加按钮并添加点击事件

作者: 发表时间:2020-04-21 21:49:31浏览量:113

Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.ClickDim btn(10) As ButtonDim count1 As Int16For count1 = 0 To 10btn(count1) = New Buttonbtn(count1).SetBounds(400, 5 + count1 * 45, 150, 40)btn(count1).Text = "我是第" & count1 + 1 & "个按钮"bt

Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click

Dim btn(10) As Button

Dim count1 As Int16

For count1 = 0 To 10

btn(count1) = New Button

btn(count1).SetBounds(400, 5 + count1 * 45, 150, 40)

btn(count1).Text = "我是第" & count1 + 1 & "个按钮"

btn(count1).Tag = count1

Me.Controls.Add(btn(count1))

AddHandler btn(count1).Click, AddressOf Me.btClick

Next


End Sub

Private Sub btClick(ByVal sender As Object, ByVal e As EventArgs)

Dim btn As Button

btn = CType(sender, Button)

MsgBox(btn.Tag.ToString.PadLeft(3, "0") & ":" & btn.Text)

End Sub



本代码通过点击button15来创建11个按钮并添加到界面中。

注意的是创建按钮的索引是利用按钮的tag属性保存的。

创建后按钮的click方法被转到btclick函数上去了

AddHandler btn(count1).Click, AddressOf Me.btClick

————————————————

版权声明:本文为CSDN博主「saddogchen」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/saddogchen/article/details/84829383




TAG:网站建设,网站推广,微信公众号开发、小程序开发、网站活动策划,H5制作,代运营,网站代运营,微信代运营,公众号代运营,网站定制,代维护
2020-04-21 21:49:31 113浏览

联系我们

好管家网络工作室
电话:18778998308
邮箱:info@esxa.cn
地址:好管家网络工作室