aaremke
aaremke

Reputation: 1

How to locate dynamically created control in VB.Net windows application?

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?

Upvotes: 0

Views: 1161

Answers (2)

Jack Gajanan
Jack Gajanan

Reputation: 1670

You need reference to other two controls in click of button

My suggestion ---

'craete a class to hold all controls in single object
Public Class MyCtls
    Public mLBL As Label
    Public mTXT As TextBox
    Public mBTN As Button

    Public Sub New(ByVal LBL As Label, ByVal TXT As TextBox, ByVal BTN As Button)
        MyBase.New()
        mLBL = LBL
        mTXT = TXT
        mBTN = BTN
    End Sub
End Class

'While creating new row create class reference and store it somewhere in accessed control
'For example we are using tag prosperity of button for this
'Now add handler for this button
Private Sub CreateNewRow()
    Dim nRow As MyCtls = New MyCtls(New Label, New TextBox, New Button)
    'set postition and add to parrent
    nRow.mBTN.Tag = nRow
    AddHandler nRow.mBTN.Click, AddressOf mBTN_Click
End Sub

'now for removing this row
Private Sub mBTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim thisRow As MyCtls = DirectCast(CType(sender, Button).Tag, MyCtls)
    'remove handler first
    RemoveHandler thisRow.mBTN.Click, AddressOf mBTN_Click

    'remove controls from its parent and dispose them
    yourparentcontrol.Controls.Remove(thisRow.mLBL)
    yourparentcontrol.Controls.Remove(thisRow.mTXT)
    yourparentcontrol.Controls.Remove(thisRow.mBTN)

    'dispose them all
    thisRow.mLBL.Dispose() 'do as this
End Sub

Upvotes: 0

pkoszka
pkoszka

Reputation: 104

I suggest you create your own user control which has all 3 elements (label, textbox, button) and add this control every time your main button is clicked. You can then connect an event handler to the button click on the 'row' control and handle it from the main form. Generic idea below:

In your user control you will need to add:

Public Event MyButtonClicked

Public Sub MyButtonClick() Handles MyButtonClick
    Raise Event MyButtonClicked(Me)
End Sub

On the main form you will have something like that

Public Sub CreateNewRow() Handles MainButton.Click
    Dim NewRow as New MyUserControl
    AddHandler NewRow.Click, AddressOf RemoveRow
    FlowLayoutPanel.Controls.Add(NewRow)
End Sub

Public Sub RemoveRow(ByRef Row as MyUserControl)
    FlowLayoutPanel.Controls.Remove(Row)
End Sub

This will let you use designer to design one row, you also can code all the functionality (like validation etc) within small 'row control' which will make your code slightly cleaner.

Upvotes: 0

Related Questions