in vb.net how do I declare a public variable from a private sub

被刻印的时光 ゝ 提交于 2019-12-25 14:10:19

问题


I am creating Labels dynamically from a private sub, and I want to be able to do something when the user clicks on them. However, I can't use "Dim withEvents blah..." because it says withEvents can't be used on a local variable but I also can't use "Public withEvents blah" from within my Private Sub. How do I accomplish this?

Thanks.


回答1:


When you create dynamic control, you can add a handler for it

Dim mylbl As New Label
mylbl.Name = "button1"
mylbl.Text = "hi"
Me.Controls.Add(mylbl)

AddHandler lbl.Click, AddressOf AllLabels_Click

This is your Handler Sub

Sub AllLabels_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim lbl As Label = CType(sender, Label)

    MsgBox(lbl.Text)

End Sub


来源:https://stackoverflow.com/questions/17119426/in-vb-net-how-do-i-declare-a-public-variable-from-a-private-sub

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!