Deleting controls created at run time...

See code snippet below:

When I click a checkbox, I have two combos placed to the right.
When I deselect that same checkbox, I'd like to destroy/remove those two corresponding combos.

How can I accomplish this?
If it helps - the checkbox's name will be a unique number - as you can see the corresponding combos will be that same number preceeded with a "P" or "S".
Private Sub chkBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
  Dim chkBox As CheckBox = DirectCast(sender, CheckBox)
  Dim cboCat, cboSub As ComboBox
  If chkBox.Checked Then
    cboCat = New CustomCombo()
    cboSub = New CustomCombo(cboCat)
    cboCat.Name = "P" & chkBox.Name
    cboSub.Name = "S" & chkBox.Name
    cboCat.Tag = cboSub
    tlpAppts.Controls.Add(cboCat, 1, intCount - 1)
    tlpAppts.Controls.Add(cboSub, 2, intCount - 1)
    'Die combos die!, but how?
  End If

Open in new window

LVL 67
Who is Participating?
PaulHewsConnect With a Mentor Commented:
>So, technically it's not being removed, right?

Your controls are on a panel instead of the form... so remove them from the panel instead of the form.

Instead of

Me.Controls.Remove ...


Dim ctl1() As Control = Me.Controls.Find("P" &, True)
Dim ctl2() As Control = Me.Controls.Find("S" &, True)
If ctl1.length > 0 then
End If

Joel CoehoornConnect With a Mentor Director of Information TechnologyCommented:
Unless you've saved a more convenient reference to the controls, you'll have to iterate through a controls array to find the correct items:

    For i As Integer = tlpAppts.Controls.Count-1 To 0 Step -1
        Dim ctrl As Control = tlpAppts.Controls(i)
        If ctrl.Name = "P" & chkBox.Name OrElse ctrl.Name = "S" & chkBox.Name Then
        End If
    Next i

I used an index variable rather than an enumerator because deleting from the list can break enumeration.
Fernando SotoConnect With a Mentor RetiredCommented:
Here is sample code on how to do it.

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
        Dim tb1 As New TextBox
        Dim tb2 As New TextBox
        tb1.Location = New Point(0, 0)
        tb1.Name = "12345"
        tb2.Location = New Point(0, 25)
        tb2.Name = "67890"
    End Sub
    Private Sub DeleteControl_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles DeleteControl.Click
        Dim tb1() As Control = Me.Controls.Find("12345", True)
        Dim tb2() As Control = Me.Controls.Find("67890", True)
        If tb1.Length > 0 Then
        End If
        If tb2.Length > 0 Then
        End If
    End Sub

Open in new window

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

sirbountyAuthor Commented:
Hmm - no errors, but the controls are still there.
I used:

Dim ctl1() As Control = Me.Controls.Find("P" &, True)
Dim ctl2() As Control = Me.Controls.Find("S" &, True)
If ... .length > 0 then...

sirbountyAuthor Commented:
Setting visible=false before removing works, but is that the correct route?
sirbountyAuthor Commented:
I take that back...the removal seemed to work the first time.
But if I click it (which brings back the two combos) and then deselect it again, I get 4 combos...
Joel CoehoornDirector of Information TechnologyCommented:
As long as ctl1.Length is 0 if you try to find it again, I think you'll be okay.
Fernando SotoRetiredCommented:
In my sample code if you click on Button1 it will add the two controls. Then if you click on DeleteControl it rwill remove them. Now if you click on Button1 twice you will need to click on DeleteControl twice. The first click on DeleteControl will remove the first set of controls and the second click will remove the second set of conteol. This is because there are two text boxes with the name "12345" and two text boxes with the name "67890". I suspect that this is what is happening with your code, "P" & and  "S" & are the names of multiple controls?
sirbountyAuthor Commented: will be equivalent to a unique GUID - I'm just preceeding them with "P" & "S" to differentiate between primary/secondary (I have a custom class that allows me to perform different 'click' routines based upon which is chosen).

Maybe I'm going about this the wrong way?

Here's a short description of what I'm doing:

I have a table that contains as many rows as there are appointments (pulled from MS Outlook).
When the user clicks one of these row items, they're presented with a couple of combo boxes to further define the appointment category (which will be eventually stored in a database).

  [ ]   Some appointment   [Combo1]  [Combo2]

The comboboxes don't appear unless that item has been checked.  And the entries aren't updated until a form submit button is pressed - allowing the user to change their mind and 'not' process that particular record.  Thus, I thought that when the checkbox was deselected, I could just remove those two controls and start over - if they changed their mind again and once more checked the box to select that record.

Hopefully that'll clear up any confusion here - and provide any further detail you might need.  If not, please let me know - and I truly appreciate the help. :^)
sirbountyAuthor Commented:
Yes, I notice the 2nd time through that my ctl1.Length is now 2, rather than 1.
So, technically it's not being removed, right?
sirbountyAuthor Commented:
Thanx Paul - I actually removed the panel long ago, but you are correct - they are sitting inside of a table (I guess I'm being too technical there, eh? :)  I'm grateful for all your help so far...

Anyway - so first things first:
If I perform a remove against the control and get no error - how come it still exists? Is it because of the whole VB.Net cleanup methods that occur in the background, and given time it would finally cease to exist?
If that's the case, I believe I remember there being a way to 'force' that cleanup to occur sooner, but not so sure it was a recommended route to take.
Secondly, I thought the "True" argument for the find function stated to search all child objects as well - wouldn't any container be a child of the form?

I'll give this a shot nonetheless.  
Should I assume that my logic in presenting these controls for this type of method is on target?
I'd be willing to try another approach if possible, I just didn't see any other way of automating the creation of multiple rows/records for those times when someone had more than just 1 appoinment in their calendar.
And there will probably be little to no times that someone would be unchecking and rechecking that box, but I figured I have to code for it...unless I simply prevent it from being deselected, without hitting the tab's 'cancel' button...that may be an easy way out...(thinking out loud here)...

Thanx again for all the experts' help!

sirbountyAuthor Commented:
By jove that's got it!
So, the "True" argument does what? : \

You've saved me again Paul - a thousand and one "thank yous"!!
If you didn't specify True in the Find method, it wouldn't find a control in a panel or other container.  Still you have to use the remove method of the controls collection of the correct container.  
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.