Solved

Deleting controls created at run time...

Posted on 2007-11-29
13
162 Views
Last Modified: 2013-11-07
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

0
Comment
Question by:sirbounty
  • 7
  • 2
  • 2
  • +1
13 Comments
 
LVL 18

Assisted Solution

by:jcoehoorn
jcoehoorn earned 25 total points
ID: 20377274
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:

    SuspendLayout()
    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
            tlpAppts.Controls.Remove(ctrl)
            ctrl.Dispose()
        End If
    Next i
    ResumeLayout()

I used an index variable rather than an enumerator because deleting from the list can break enumeration.
0
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 175 total points
ID: 20377306
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"

        Me.Controls.Add(tb1)

        tb2.Location = New Point(0, 25)

        tb2.Name = "67890"

        Me.Controls.Add(tb2)

    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

            Me.Controls.Remove(tb1(0))

        End If

        If tb2.Length > 0 Then

            Me.Controls.Remove(tb2(0))

        End If
 

    End Sub

Open in new window

0
 
LVL 67

Author Comment

by:sirbounty
ID: 20377397
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))

0
 
LVL 67

Author Comment

by:sirbounty
ID: 20377414
Setting visible=false before removing works, but is that the correct route?
0
 
LVL 67

Author Comment

by:sirbounty
ID: 20377428
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...
0
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 20377429
As long as ctl1.Length is 0 if you try to find it again, I think you'll be okay.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 62

Expert Comment

by:Fernando Soto
ID: 20377536
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?
0
 
LVL 67

Author Comment

by:sirbounty
ID: 20377803
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. :^)
0
 
LVL 67

Author Comment

by:sirbounty
ID: 20377829
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?
0
 
LVL 38

Accepted Solution

by:
PaulHews earned 300 total points
ID: 20377920
>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 ...

Use

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

0
 
LVL 67

Author Comment

by:sirbounty
ID: 20378564
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!

0
 
LVL 67

Author Comment

by:sirbounty
ID: 20378585
By jove that's got it!
So, the "True" argument does what? : \

You've saved me again Paul - a thousand and one "thank yous"!!
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 20378677
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.  
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Word Template Mail merge with vb.net 4 40
Trouble with References... 5 26
Get String split 5 33
Achieve json result 2 35
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now