且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在VB.Net windows应用程序中找到动态创建的控件?

更新时间:2023-12-06 10:02:52

解决方法如下:



私有 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.