我正在尝试通过TextBox
事件删除我的表单中的Button_Click
控件(动态添加)(我也动态添加)但我找不到确切的方法来做到这一点。单击LinkLabel
时,我的TextBox将与Button控件(删除按钮)一起添加。因此,当动态添加时,我的textbox.name
将类似于textbox_1
,textbox_2
,textbox_3
,同时它们也是一个像btnDel1
,btnDel2
这样的Button控件,btnDel3
(全部放在Panel控件中)。
我的编码是这样的:
Private Sub Button_Click(sender As Object, e As EventArgs)
Dim button As Button = TryCast(sender, Button)
Dim textbox As TextBox = TryCast(sender, TextBox)
'In this case when btnDel1 is clicked, textbox_1 will be removed as well
If button.Name = "btnDel1" Then
PanelOthers.Controls.Remove(button)
End If
End Sub
按钮已成功删除,但如何删除文本框?提前谢谢。
答案 0 :(得分:1)
有几种方法可以做到这一点:
创建控件时,将关联的控件添加到按钮的.Tag
属性:
Dim button As Button = New Button
Dim textbox As TextBox = New TextBox
button.Tag = {textbox}
' Add the button and textbox to the UI surface
现在,当单击该按钮时,您可以循环关联的控件并将其删除:
For Each item As Control In button.Tag
item.Dispose()
Next
button.Dispose()
所以不是教程网站..但你可以自己研究这个。