Unable to add and Delete controls Programmatically


New member
Nov 24, 2021
Programming Experience
Hi there !!

I am currently working in a desktop based application where I have to add controls programmatically and then to get the name of the control to remove it.

Now the issue which I am facing is the name for the control changes as I add more controls to the form and I am unable to remove it as the name is common for all the controls.

Which I need is I want to add multiple panels with the different names and then remove them with the click of a button programmatically.

Please find attached the current code to look at :

' Defining the controls at the beginning

Dim base_panel As Panel
Dim start_pause As Button
Dim Timer_label As Label
Dim Data_label As Label
Dim Delete_task As Button
Dim array_ID As Integer()
Dim current_Count As Integer = 0

'Adding components Dynamically through this Sub

Sub component_addition(ByRef ID As Integer)
'Adding the components programatically
base_panel = New Panel
Timer_label = New Label
Data_label = New Label
Delete_task = New Button
start_pause = New Button
'Make the current ID
current_Count = ID
'Making the names for the diferent items
base_panel.Name = current_Count.ToString

start_pause.Name = current_Count.ToString
Timer_label.Name = current_Count.ToString
Data_label.Name = current_Count.ToString
Delete_task.Name = current_Count.ToString
'Setting the contorls in the panel and making them visible in the main app
' Here need to change some items
Data_label.Text = TextBox1.Text
Timer_label.Text = "00:00"
Delete_task.Text = "X"
start_pause.Text = "<>"
'Adding Data into the Panel
Data_label.Location = New Point(10, 10)
'Adding Timer Label into the Panel
Timer_label.Location = New Point(130, 10)
'Adding Delete Task into the panel
Delete_task.Location = New Point(250, 10)
Delete_task.Size() = New Size(30, 25)
'Adding Start Pause Button in the Panel
start_pause.Location = New Point(370, 10)
AddHandler start_pause.Click, AddressOf aaa
start_pause.Size() = New Size(30, 25)
base_panel.BackColor = Color.AliceBlue
base_panel.Size = New Size(600, 40)
'base_panel.Anchor = AnchorStyles.Left And AnchorStyles.Right And AnchorStyles.Top And AnchorStyles.Bottom
base_panel.Location = New Point(0, ID * 30)
End Sub

'This button adds the panel and the button etc at the time of runtime.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles addtask.Click
' Dim btn As Button
' btn = New Button
' btn.Location = New Point(i 10, i 10)
' homepanel.Controls.Add(btn)
' i = i + 1

current_Count = current_Count + 1
End Sub

'This is the sub which is being called to get the name of the panel

Sub removeitems()
End Sub

Thanks in advance for any suggestions.
Last edited by a moderator:


VB.NET Forum Moderator
Staff member
Aug 17, 2004
Sydney, Australia
Programming Experience
If you want to remove a specific Panel then you obviously need to be able to uniquely identify it. How exactly do you propose to do that? Is the user clicking a Button on that Panel to remove it? In that case, the sender parameter will refer to the Button in its Click event handler and you can get the Panel from its Parent property.

I would suggest that you're doing this all wrong though. Don't create a Panel and lots of child controls every time. Start by designing a user control with all the child controls and all the logic associated with them. You add a user control to your project much as you do a form and you design it and add code the same way too. When you build your project, that user control will be added to the Toolbox. You can now use it in either the designer and in code in exactly the same way as you would any other control, so your form code will be simplified considerably. You just need to add an instance of the user control and handle and events you added to it.
Top Bottom