Link to home
Start Free TrialLog in
Avatar of sirbounty
sirbountyFlag for United States of America

asked on

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)
  Else
    'Die combos die!....er, but how?
  End If

Open in new window

SOLUTION
Avatar of Joel Coehoorn
Joel Coehoorn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sirbounty

ASKER

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

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

Setting visible=false before removing works, but is that the correct route?
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...
As long as ctl1.Length is 0 if you try to find it again, I think you'll be okay.
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" & sender.name and  "S" & sender.name are the names of multiple controls?
sender.name 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. :^)
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?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!

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.