如何在VB.Net windows应用程序中找到动态创建的控件? [英] How to locate dynamically created control in VB.Net windows application?
问题描述
我有一个TableLayoutPanel(有一行,三列),它放在窗体上的Panel控件中。我的表单也有一个命令按钮。每次单击该按钮时,将动态创建标签(在第一列中),文本框(在第二列中)和一个按钮(在第三列中)。我想执行如下操作:
当我单击按钮(在每行的第三列)时,必须删除相关行的LABEL + TEXTBOX + BUTTON,同时保留其他控件。有人可以帮我解决吗?
解决方法如下:
< pre lang =vb> 私有 Sub Label1_Click( ByVal 发件人作为 对象, ByVal e As EventArgs)句柄 Label1.Click
Dim myTextBox As 新 TextBox
Dim myButton As 新按钮
AddHandler myButton.Click, AddressOf ButtonClick
myTLP.Controls.Add(myTextBox, 1 , 0 )
myTLP.Controls.Add(myButton, 2 , 0 )
结束 Sub
私有 Sub ButtonClick( ByVal sender 作为 对象, ByVal e As EventArgs)
Dim tmpbtn As Button = TryCast (发件人,按钮)
如果 不 tmpbtn 没什么 然后
对于 i As 整数 = 0 至 myTLP.ColumnCount - 1
Dim tmpctr As Control = _
myTLP.GetControlFromPosition(i,myTLP.GetRow(tmpbtn))
If 不 tmpctr 没什么 然后 myTLP.Controls.Remove(tmpctr)
下一步
结束 如果
结束 Sub
为每个创建的按钮添加相同的处理程序,它将相应删除。
只需确保设置ColumnCount和RowC TableLayoutPanel的数量。
I have one TableLayoutPanel (having one row, three columns) which is placed inside a Panel control on the form. My form also have one command button. Every time when the button is clicked a label (in first column), a textbox (in the second column), and a button (in the third column) will be created dynamically. I want to perform a operation like following:
When i click button (in third column of each row) then LABEL+TEXTBOX+BUTTON of concerned row must be deleted while leaving other controls as is. Could anybody help me out to resolve?
The solution for this would be as follow
Private Sub Label1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Label1.Click Dim myTextBox As New TextBox Dim myButton As New Button AddHandler myButton.Click, AddressOf ButtonClick myTLP.Controls.Add(myTextBox, 1, 0) myTLP.Controls.Add(myButton, 2, 0) End Sub Private Sub ButtonClick(ByVal sender As Object, ByVal e As EventArgs) Dim tmpbtn As Button = TryCast(sender, Button) If Not tmpbtn Is Nothing Then For i As Integer = 0 To myTLP.ColumnCount - 1 Dim tmpctr As Control = _ myTLP.GetControlFromPosition(i, myTLP.GetRow(tmpbtn)) If Not tmpctr Is Nothing Then myTLP.Controls.Remove(tmpctr) Next End If End Sub
Add the same handler for each of the created button and it will delete accordingly.
Just make sure you set the ColumnCount and RowCount for the TableLayoutPanel.
这篇关于如何在VB.Net windows应用程序中找到动态创建的控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!