473,407 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Controls.Remove not removing ALL controls.

I have the following code the dynamically adds a specific number of controls.

for x as integer = 1 to 10
Dim btn as Windows.Forms.Button = New Windows.Forms.Button
btn.Name = "btn" & x
btn.Text = "Test" & x
controls.add(btn)
next x

This results in 10 buttons appearing on the screen. I've excluded the
location info.

Now, in my program the number of buttons varies based on the size of the
form. If the form is decreased in size I will display less buttons. But,
before I update the buttons on the screen, I just remove them all before
refreshing the screen with new buttons.

I use the following code to remove the buttons on the screen.

For Each btn as Windows.Forms.Button in Me.Controls
controls.remove(btn)
Next

In my example, I have 48 buttons displayed on the screen. When the Remove
code above runs it does not delete/remove all of the buttons. If I replace
the controls.Remove command with a debug.print command I can see that every
button is found in the loop. But, when I replace the debug.print and put the
conttols.remove back in all of the buttons are NOT removed. My observation is
that every other button is being removed instead.

Is there any reason why I would be getting this behavior? Is there any
reason why the controls.remove command would not remove a specific button?

I suspect more informaiton is needed on this, but what I'm doing is pretty
straight forward, so I have nothing else to post.

Any help is appreciated.

Greg
Feb 17 '08 #1
2 9051
Greg wrote:
>
I use the following code to remove the buttons on the screen.

For Each btn as Windows.Forms.Button in Me.Controls
controls.remove(btn)
Next

My observation is that every other button is
being removed instead.

Is there any reason why I would be getting this behavior? Is there any
reason why the controls.remove command would not remove a specific
button?
The interator of the For loop (btn) gets lost. If you just have a debug
statement, the contents of the collection doesn't change during the loop. If you
are removing the buttons, though, the content does change, especially if you
remove the control the iterator is currently "on".

The "every other one" effect is because there is no current btn when it gets to
the Next, so the next btn becomes the current btn, and the one after that
becomes the next btn. It is generally true of collections.
Feb 17 '08 #2
On Sun, 17 Feb 2008 00:04:00 -0800, Greg
<Ac**********@newsgroups.nospamwrote:
>I have the following code the dynamically adds a specific number of controls.

for x as integer = 1 to 10
Dim btn as Windows.Forms.Button = New Windows.Forms.Button
btn.Name = "btn" & x
btn.Text = "Test" & x
controls.add(btn)
next x

This results in 10 buttons appearing on the screen. I've excluded the
location info.

Now, in my program the number of buttons varies based on the size of the
form. If the form is decreased in size I will display less buttons. But,
before I update the buttons on the screen, I just remove them all before
refreshing the screen with new buttons.

I use the following code to remove the buttons on the screen.

For Each btn as Windows.Forms.Button in Me.Controls
controls.remove(btn)
Next

In my example, I have 48 buttons displayed on the screen. When the Remove
code above runs it does not delete/remove all of the buttons. If I replace
the controls.Remove command with a debug.print command I can see that every
button is found in the loop. But, when I replace the debug.print and put the
conttols.remove back in all of the buttons are NOT removed. My observation is
that every other button is being removed instead.

Is there any reason why I would be getting this behavior? Is there any
reason why the controls.remove command would not remove a specific button?

I suspect more informaiton is needed on this, but what I'm doing is pretty
straight forward, so I have nothing else to post.

Any help is appreciated.

Greg
Don't change a list while traversing it with an iterator (For Each).
That at a minimum confuses the iterator and at worst gives you an
exception.

You could remove all of the buttons with:
controls.Clear()

But you really should dispose of the buttons so that the Window
handles for the controls get released right away:

For indx As Integer = Controls.Count-1 To 0 Step -1
Dim ctl As Control

ctl = Controls.Item(indx)
Controls.Remove(ctl)
ctl.Dispose()
Next
Feb 17 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: mawi | last post by:
Hello, Description: I create panels with some controls on a page using a new panel button. One of the controls on each panel is the "close panel" button that is supposed to close the panel it...
9
by: Merlin | last post by:
Hi, My code below doesn't work does anyone have any pointers? All my controls are programically added. Dim i As Int16 For i = 0 To Me.Controls.Count - 1 If Me.Controls(i).Name <>...
66
by: Cor | last post by:
Hi, I start a new thread about a discussion from yesterday (or for some of us this morning). It was a not so nice discussion about dynamically removing controls from a panel or what ever. It...
7
by: Scott Emick | last post by:
How can I disable events for the controls on a form? I tried setting the form's enable property to false, but that doesn't stop events from firing on its controls. I need to temporarily disable...
6
by: Peter Zolja | last post by:
Hi, I'm building a webcontrol that contains a dynamic list of other controls. My problem is that when I add or remove an item the synchronization between the ViewState and the Controls...
2
by: Cerebrus99 | last post by:
Hi all, I am dynamically removing some child controls from a groupbox. The problem occurs because some controls are removed and some aren't. Dim ctl as Control For each ctl in...
8
by: hunanwarrior | last post by:
I added textbox controls to a form when user selects amount to create from a combobox as follows: 'Load up the combobox Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As...
1
by: abyclassic | last post by:
Dear All, I am making a Windows Control Library(user control) using VS.NET & C#.NET. Now, i am facing 3 problems: 1.In this control, i wants to remove some of the properties which are available...
32
by: =?Utf-8?B?U2l2?= | last post by:
I have a form that I programmatically generate some check boxes and labels on. Later on when I want to draw the form with different data I want to clear the previously created items and then put...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.