本文介绍了在 vb.net 中,如何从私有子节点声明公共变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在从私有子节点动态创建标签,并且我希望能够在用户单击它们时执行某些操作.但是,我不能使用Dim withEvents blah ...",因为它说 withEvents 不能用于局部变量,但我也不能在我的 Private Sub 中使用Public withEvents blah".我该如何实现?
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?
谢谢.
推荐答案
创建动态控件时,可以为其添加处理程序
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
这是您的处理程序子
Sub AllLabels_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim lbl As Label = CType(sender, Label)
MsgBox(lbl.Text)
End Sub
这篇关于在 vb.net 中,如何从私有子节点声明公共变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!