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 :
Thanks in advance for any suggestions.
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 :
VB.NET:
' 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)
base_panel.Controls.Add(Data_label)
'Adding Timer Label into the Panel
Timer_label.Location = New Point(130, 10)
base_panel.Controls.Add(Timer_label)
'Adding Delete Task into the panel
Delete_task.Location = New Point(250, 10)
Delete_task.Size() = New Size(30, 25)
base_panel.Controls.Add(Delete_task)
'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.Controls.Add(start_pause)
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)
homepanel.Controls.Add(base_panel)
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
component_addition(current_Count)
current_Count = current_Count + 1
End Sub
'This is the sub which is being called to get the name of the panel
Sub removeitems()
MsgBox(base_panel.Name)
End Sub
Thanks in advance for any suggestions.
Last edited by a moderator: