Question dynamically removing Controls from FORM

rehan.azher

Member
Joined
Jan 1, 2009
Messages
11
Programming Experience
Beginner
Dear Experts,

I have one form with TabControl on which i need to add/remove on same position about 37 labels and buttons after each time user click a button, bases on selection .

Problem i am facing is that when I click the button it creates the new controls and removes the old controls set, but graphics of old controls is not removed on first click , I need to select same criteria 5 to 6 times and each time graphics of controls is changed partially .

I am using following code to remove the controls :

VB.NET:
For Each ctl As Control In Me.TabControl1.TabPages(0).Controls
    If TypeName(ctl) = "Button" Or TypeName(ctl) = "Label" Then
        Me.TabControl1.TabPages(0).Controls.Remove(ctl)
    End If
Next

for Adding Control I am Using following Code:
VB.NET:
temp(j) = New Label
temp(j).Location = New System.Drawing.Point(500, y)
temp(j).Size = New System.Drawing.Size(150, 20)
temp(j).TextAlign = ContentAlignment.MiddleLeft
temp(j).ForeColor = System.Drawing.Color.Black
temp(j).BorderStyle = BorderStyle.FixedSingle
temp(j).BackColor = System.Drawing.Color.LightSalmon
temp(j).Text = j & ": " & .myDataset.Tables(0).Rows(i)(2)
Me.TabControl1.TabPages(0).Controls.Add(temp(j))
temp2(j) = New Button
temp2(j).Location = New System.Drawing.Point(650, y)
temp2(j).Size = New System.Drawing.Size(60, 20)
temp2(j).TextAlign = ContentAlignment.MiddleCenter
temp2(j).ForeColor = System.Drawing.Color.Black
temp2(j).BackColor = System.Drawing.Color.LightBlue
temp2(j).Text = "Edit Board"
temp2(j).Name = j
Me.TabControl1.TabPages(0).Controls.Add(temp2(j))
AddHandler temp2(j).Click, AddressOf Button_Click


Note again controls are removed but thier graphics still appear on the form. some method that can repaint my tabcontrol graphics.
 
Last edited by a moderator:
Resolved: Dynamically Removing controls from FORM

Hi All,

I just got my problem resolved , I changed the code to remove controls:

VB.NET:
For Each ctl2 As Control In Me.TabControl1.TabPages(0).Controls
If (TypeOf ctl2 Is Button) Then
ctl2.Visible = False

End If
Next
For Each ctl2 As Control In Me.TabControl1.TabPages(0).Controls
If (TypeOf ctl2 Is Label) Then
ctl2.Visible = False

End If
Next

but still i am confused why my previous code was not working.

Thanks JOHN for editing my previous post for readability purpose.
 
Did you check what TypeName(ctl) actually did return? If it wasn't returning what you expected then the logical thing to do would have been to check what it did return. I think you'd have found that it was returning the fully qualified name, e.g. "System.Windows.Forms.Button".

Also, there's not much point looping through the same collection of controls twice is there? You can simply test whether each control is a Button OR a Label in the same loop:
VB.NET:
If TypeOf ctl Is Button OrElse TypeOf ctl Is Label Then
Finally, if you actually do want to remove a control rather than hide it, you can't use a For Each loop. You cannot enumerate a collection using a For Each loop and then modify that collection. If you want to modify the collection then you must use a For loop and, most likely, iterate backwards:
VB.NET:
For index As Integer = myCollection.Count - 1 To 0 Step -1
 
Dear jmcilhinney,

can you please help me for the same, as I am unable to find a way to iterate through using your method , how will I access the control.


thanks
 
VB.NET:
Dim ctls As Control.ControlCollection = Me.TabControl1.TabPages(0).Controls
Dim ctl As Control

For index As Integer = ctls.Count - 1 To 0 Step -1
    ctl = ctls(index)

    If TypeOf ctl Is Button OrElse _
       TypeOf ctl Is Label Then
        ctls.Remove(ctl)
    End If
Next
 
Back
Top