Programatically remove controls from a winform tab page


Author
Message
Michael R Poirier
Michael R Poirier
StrataFrame Beginner (11 reputation)StrataFrame Beginner (11 reputation)StrataFrame Beginner (11 reputation)StrataFrame Beginner (11 reputation)StrataFrame Beginner (11 reputation)StrataFrame Beginner (11 reputation)StrataFrame Beginner (11 reputation)StrataFrame Beginner (11 reputation)StrataFrame Beginner (11 reputation)
Group: StrataFrame Users
Posts: 9, Visits: 131
I have code that adds controls to a tab page - and the code below attempts to remove them.  All of the labels are removed - but the text boxes and combo boxes are not.  I even tried unbinding the text boxes and combo boxes - still not removed.

Any help is appreciated - code to remove controls:

Private Sub clearconditiontab()

Dim sftextbox As MicroFour.StrataFrame.UI.Windows.Forms.Textbox

Dim sfcombobox As MicroFour.StrataFrame.UI.Windows.Forms.ComboBox

For Each locontrol As Control In Me.TBP_Condition.Controls

If locontrol.GetType() Is GetType(MicroFour.StrataFrame.UI.Windows.Forms.ComboBox) Then

sfcombobox = locontrol

sfcombobox.BusinessObject = Nothing

sfcombobox.BindingField = Nothing

End If

If locontrol.GetType() Is GetType(MicroFour.StrataFrame.UI.Windows.Forms.Textbox) Then

sftextbox = locontrol

sftextbox.BusinessObject = Nothing

sftextbox.BindingField = Nothing

End If

Me.TBP_Condition.Controls.Remove(locontrol)

Next

Me.TBP_Condition.Refresh()

End Sub


Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.3K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
I'd expect this to through an exception, as messing with a collection during a for each is usually not allowed. Typically I'd use a for loop and loop backwards:



For idx As Integer = Me.TBP_Condition.Controls.Count - 1 to 0 Step -1

  '-- Conditional code to determine if control needs to be removed

  '-- Then code to remove it

Next




I don't see any conditional code in your code though and if that's the case then this is a lot easier.



Me.TBP_Condition.Controls.Clear()

Michael R Poirier
Michael R Poirier
StrataFrame Beginner (11 reputation)StrataFrame Beginner (11 reputation)StrataFrame Beginner (11 reputation)StrataFrame Beginner (11 reputation)StrataFrame Beginner (11 reputation)StrataFrame Beginner (11 reputation)StrataFrame Beginner (11 reputation)StrataFrame Beginner (11 reputation)StrataFrame Beginner (11 reputation)
Group: StrataFrame Users
Posts: 9, Visits: 131
Correct - no logic -all controls have to go.

I'll try the .clear() method.

If there were conditional code -only some controls go - do I use the .remove?

I did get the process to work by first creating 1 array for each type of control (label, textbox, combobox), each element was an object reference to a control I'd created of that type - then when time to clear the page I run through the array and use the .remove(control) method - much more cumbersome than .clear() - but would be useful if I had to conditionally remove controls.  - suggestions on a better way are always welcome.

Thanks for the .clear

Mike

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.3K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Yes, to remove a single control use .Remove().



There are tons of ways you can do this. The simplest is to just loop through the controls, using index, from max to 0. Do conditional and remove if needed. I'd use this if most to all of the controls are eligible to be removed and in a one to a few known container controls. As the percentage of controls that could be removed is reduced or if the controls are contained in several controls (rather than just one tab), then the loop is looking less and less appealing.



Instead of looping, you could maintain a List(Of Control) that tracks the controls that are to be removed. This sort of thing works best if you are programatically adding the controls in the first place, as you can just add them to the list when they are created. That way you never loop through all the controls, just the controls that are eligible to be removed in the first place. This assumes that the same set is being added/removed. You could use other IEnumerable types, like an Array, or ArrayList. I like the generic one, because I get a Control out of it and don't need to do any casting.



Another option would be to use a extender provider (implementing IExtenderProvider and using the ProvideProperty attribute). This uses the same type of programming as the TooltipProvider or ErrorProvider (of even a FlowLayoutPanel). When you use a component like this, it adds one or more properties to controls. This is a bit more complex, but I'd likely use this if the controls affected were scattered all over the form, in different tabs/panels etc. and I wanted to do things declaratively via the designer. Likely, this is not your situation, but if you want more info, let me know.



Hope that helps! BigGrin
Michael R Poirier
Michael R Poirier
StrataFrame Beginner (11 reputation)StrataFrame Beginner (11 reputation)StrataFrame Beginner (11 reputation)StrataFrame Beginner (11 reputation)StrataFrame Beginner (11 reputation)StrataFrame Beginner (11 reputation)StrataFrame Beginner (11 reputation)StrataFrame Beginner (11 reputation)StrataFrame Beginner (11 reputation)
Group: StrataFrame Users
Posts: 9, Visits: 131
Thanks much for the help.  THe extender provider might come in handy for other stuff I'm doing.

MIke

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.3K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Glad you go it working.
StrataFrame Beginner (1 reputation)StrataFrame Beginner (1 reputation)StrataFrame Beginner (1 reputation)StrataFrame Beginner (1 reputation)StrataFrame Beginner (1 reputation)StrataFrame Beginner (1 reputation)StrataFrame Beginner (1 reputation)StrataFrame Beginner (1 reputation)StrataFrame Beginner (1 reputation)
Group: Forum Members
Posts: 1, Visits: 1
Here is my code
-------------------------------------------

For Each oControl As Control In pnlCoupon.Controls
    If TypeOf oControl Is UserControl Then
        Me.pnlCoupon.Controls.Remove(oControl)
     End If
Next

I have like 0 to 5 usercontrol in the panel now when use this code to remove those user control it is doing something crazy it removes uc0,uc2,uc4 and it keeps uc1,uc3,uc5, don't understand why??

any help would be appreciated!!!

Thanks
Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
You are removing the items from the collection you are enumerating.  Add it to a List<Control> or an array and then enumerate the array (or list) after your loop.  This way you are not removing from the collection you are enumerating.
Ivan George Borges
Ivan George Borges
Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)
Group: StrataFrame MVPs
Posts: 1.9K, Visits: 21K
I guess if you enumerate your controls, as soon as you remove one of them, the controls' indexex for all the others will change, which would cause the symptoms you are seeing. So, if you have index 0, 1, 2, 3, 4 and 5, as soon as you remove the 0 one, all the others decrease the value of 1, and each will become 0, 1, 2, 3 and 4. Then, moving to the next one would be the current 1, which was, formerly, the 2.
Maybe if you got the total number of controls first, and then went into a decreasing loop from the last to the first, all controls would be removed.
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search