﻿<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>StrataFrame Forum » StrataFrame Application Framework - V1 » WinForms (How do I?)  » Programatically remove controls from a winform tab page</title><generator>InstantForum 2017-1 Final</generator><description>StrataFrame Forum</description><link>http://forum.strataframe.net/</link><webMaster>StrataFrame Forum</webMaster><lastBuildDate>Fri, 26 Jun 2026 02:49:29 GMT</lastBuildDate><ttl>20</ttl><item><title>Programatically remove controls from a winform tab page</title><link>http://forum.strataframe.net/FindPost25212.aspx</link><description>I have code that adds controls to a tab page - and the code below attempts to remove them.&amp;nbsp; All of the labels are removed - but the text boxes and combo boxes are not.&amp;nbsp; I even tried unbinding the text boxes and combo boxes - still not removed.&lt;/P&gt;&lt;P&gt;Any help is appreciated - code to remove controls:&lt;/P&gt;&lt;P&gt;&lt;FONT color=#0000ff&gt;Private&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/FONT&gt; clearconditiontab()&lt;/P&gt;&lt;P&gt;&lt;FONT color=#0000ff&gt;Dim&lt;/FONT&gt; sftextbox &lt;FONT color=#0000ff&gt;As&lt;/FONT&gt; MicroFour.StrataFrame.UI.Windows.Forms.Textbox&lt;/P&gt;&lt;P&gt;&lt;FONT color=#0000ff&gt;Dim&lt;/FONT&gt; sfcombobox &lt;FONT color=#0000ff&gt;As&lt;/FONT&gt; MicroFour.StrataFrame.UI.Windows.Forms.ComboBox&lt;/P&gt;&lt;P&gt;&lt;FONT color=#0000ff&gt;For&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Each&lt;/FONT&gt; locontrol &lt;FONT color=#0000ff&gt;As&lt;/FONT&gt; Control &lt;FONT color=#0000ff&gt;In&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Me&lt;/FONT&gt;.TBP_Condition.Controls&lt;/P&gt;&lt;P&gt;&lt;FONT color=#0000ff&gt;If&lt;/FONT&gt; locontrol.GetType() &lt;FONT color=#0000ff&gt;Is&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;GetType&lt;/FONT&gt;(MicroFour.StrataFrame.UI.Windows.Forms.ComboBox) &lt;FONT color=#0000ff&gt;Then&lt;/P&gt;&lt;/FONT&gt;&lt;P&gt;sfcombobox = locontrol&lt;/P&gt;&lt;P&gt;sfcombobox.BusinessObject = &lt;FONT color=#0000ff&gt;Nothing&lt;/P&gt;&lt;/FONT&gt;&lt;P&gt;sfcombobox.BindingField = &lt;FONT color=#0000ff&gt;Nothing&lt;/P&gt;&lt;/FONT&gt;&lt;P&gt;&lt;FONT color=#0000ff&gt;End&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;If&lt;/P&gt;&lt;/FONT&gt;&lt;P&gt;&lt;FONT color=#0000ff&gt;If&lt;/FONT&gt; locontrol.GetType() &lt;FONT color=#0000ff&gt;Is&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;GetType&lt;/FONT&gt;(MicroFour.StrataFrame.UI.Windows.Forms.Textbox) &lt;FONT color=#0000ff&gt;Then&lt;/P&gt;&lt;/FONT&gt;&lt;P&gt;sftextbox = locontrol&lt;/P&gt;&lt;P&gt;sftextbox.BusinessObject = &lt;FONT color=#0000ff&gt;Nothing&lt;/P&gt;&lt;/FONT&gt;&lt;P&gt;sftextbox.BindingField = &lt;FONT color=#0000ff&gt;Nothing&lt;/P&gt;&lt;/FONT&gt;&lt;P&gt;&lt;FONT color=#0000ff&gt;End&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;If&lt;/P&gt;&lt;/FONT&gt;&lt;P&gt;&lt;FONT color=#0000ff&gt;Me&lt;/FONT&gt;.TBP_Condition.Controls.Remove(locontrol)&lt;/P&gt;&lt;P&gt;&lt;FONT color=#0000ff&gt;Next&lt;/P&gt;&lt;/FONT&gt;&lt;P&gt;&lt;FONT color=#0000ff&gt;Me&lt;/FONT&gt;.TBP_Condition.Refresh()&lt;/P&gt;&lt;P&gt;&lt;FONT color=#0000ff&gt;End&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/P&gt;&lt;/FONT&gt;</description><pubDate>Mon, 05 Dec 2011 14:34:37 GMT</pubDate><dc:creator>Michael R Poirier</dc:creator></item><item><title>RE: Programatically remove controls from a winform tab page</title><link>http://forum.strataframe.net/FindPost30684.aspx</link><description>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.&lt;br/&gt;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.</description><pubDate>Mon, 05 Dec 2011 14:34:37 GMT</pubDate><dc:creator>Ivan George Borges</dc:creator></item><item><title>RE: Programatically remove controls from a winform tab page</title><link>http://forum.strataframe.net/FindPost30683.aspx</link><description>You are removing the items from the collection you are enumerating.&amp;nbsp; Add it to a List&amp;lt;Control&amp;gt; or an array and then enumerate the array (or list) after your loop.&amp;nbsp; This way you are not removing from the collection you are enumerating.</description><pubDate>Mon, 05 Dec 2011 14:29:37 GMT</pubDate><dc:creator>Trent L. Taylor</dc:creator></item><item><title>RE: Programatically remove controls from a winform tab page</title><link>http://forum.strataframe.net/FindPost30682.aspx</link><description>Here is my code&lt;br/&gt;-------------------------------------------&lt;br/&gt;&lt;br/&gt;For Each oControl As Control In pnlCoupon.Controls&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; If TypeOf oControl Is UserControl Then&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Me.pnlCoupon.Controls.Remove(oControl)&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End If&lt;br/&gt;Next&lt;br/&gt;&lt;br/&gt;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??&lt;br/&gt;&lt;br/&gt;any help would be appreciated!!!&lt;br/&gt;&lt;br/&gt;Thanks </description><pubDate>Mon, 05 Dec 2011 14:06:21 GMT</pubDate><dc:creator /></item><item><title>RE: Programatically remove controls from a winform tab page</title><link>http://forum.strataframe.net/FindPost25275.aspx</link><description>Glad you go it working.</description><pubDate>Wed, 25 Nov 2009 11:26:23 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item><item><title>RE: Programatically remove controls from a winform tab page</title><link>http://forum.strataframe.net/FindPost25270.aspx</link><description>Thanks much for the help.&amp;nbsp; THe extender provider might come in handy for other stuff I'm doing.&lt;/P&gt;&lt;P&gt;MIke</description><pubDate>Tue, 24 Nov 2009 20:43:30 GMT</pubDate><dc:creator>Michael R Poirier</dc:creator></item><item><title>RE: Programatically remove controls from a winform tab page</title><link>http://forum.strataframe.net/FindPost25222.aspx</link><description>Yes, to remove a single control use .Remove().&lt;br&gt;
&lt;br&gt;
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. &lt;br&gt;
&lt;br&gt;
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.&lt;br&gt;
&lt;br&gt;
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.&lt;br&gt;
&lt;br&gt;
Hope that helps! :D</description><pubDate>Mon, 16 Nov 2009 22:09:57 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item><item><title>RE: Programatically remove controls from a winform tab page</title><link>http://forum.strataframe.net/FindPost25221.aspx</link><description>Correct - no logic -all controls have to go.&lt;/P&gt;&lt;P&gt;I'll try the .clear() method.&lt;/P&gt;&lt;P&gt;If there were conditional code -only some controls go - do I use the .remove?&lt;/P&gt;&lt;P&gt;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.&amp;nbsp; - suggestions on a better way are always welcome.&lt;/P&gt;&lt;P&gt;Thanks for the .clear &lt;/P&gt;&lt;P&gt;Mike</description><pubDate>Mon, 16 Nov 2009 20:34:36 GMT</pubDate><dc:creator>Michael R Poirier</dc:creator></item><item><title>RE: Programatically remove controls from a winform tab page</title><link>http://forum.strataframe.net/FindPost25213.aspx</link><description>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:&lt;br&gt;
&lt;br&gt;
[codesnippet]For idx As Integer = Me.TBP_Condition.Controls.Count - 1 to 0 Step -1&lt;br&gt;
&amp;nbsp;&amp;nbsp;'-- Conditional code to determine if control needs to be removed&lt;br&gt;
&amp;nbsp;&amp;nbsp;'-- Then code to remove it&lt;br&gt;
Next[/codesnippet]&lt;br&gt;
&lt;br&gt;
I don't see any conditional code in your code though and if that's the case then this is a lot easier.&lt;br&gt;
&lt;br&gt;
[codesnippet]Me.TBP_Condition.Controls.Clear()[/codesnippet]</description><pubDate>Mon, 16 Nov 2009 09:36:59 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item></channel></rss>