我有14个标签,名为Label_1,Label_2 .... Label_14 ..等, 我想检测哪一个是点击。
此代码可以运行:
Private Sub Label_1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label_2.Click, Label_3.Click, Label_4.Click, Label_5.Click, Label_6.Click, Label_7.Click, Label_8.Click, Label_9.Click, Label_10.Click, Label_11.Click, Label_12.Click, Label_13.Click, Label_14.Click
Dim enent_text As String = (Strings.Split(CType(sender, Label).Text, "_")(0))
MessageBox.Show("clcik: " & enent_text)
End Sub
但是如何删除Handles?如果我将来会有更多的标签(例如:50), 防止输入标签。我不想分配标签。
Private Sub Label_1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label_2.Click, Label_3.Click, Label_4.Click, Label_5.Click, Label_6.Click, Label_7.Click, Label_8.Click, Label_9.Click, Label_10.Click, Label_11.Click, Label_12.Click, Label_13.Click, Label_14.Click, Label_15.Click, Label_16.Click, Label_17.Click, Label_18.Click...., Label_50.Click
我只是想成为这个,删除所有Handles的项目,我希望它可以运行。
Private Sub Label_1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim enent_text As String = (Strings.Split(CType(sender, Label).Text, "_")(0))
MessageBox.Show("clcik: " & enent_text)
End Sub
我该怎么办?感谢。
答案 0 :(得分:0)
听起来你应该动态创建控件,类似于旧的VB6控件数组。您可以分配单个处理程序,并根据需要添加或删除控件。见How to create Control Arrays in VB .NET