473,386 Members | 1,785 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,386 software developers and data experts.

VB .NET Strange Behaviour When Removing Form Controls

sgeklor
Hi guys,

I have a panel on a form and at runtime I create some controls on the panel. Then, also during runtime I want to clear the panel of all of its controls. The basic way to do this is with the following code:

Expand|Select|Wrap|Line Numbers
  1. For Each this_ctrl As Object In target_panel.Controls
  2.      this_ctrl.Dispose()
  3. Next
What happens when I use this code is that only every second control is removed from the panel! Yet, when I replace the .Dispose() method with some other behaviour (like change the control's colour) it appears that every control is accessed correctly. Repeating the code block iteratively removes every second control until none are left, but this is hardly an elegant way to proceed.

So I think this is strange. Can anyone suggest a cause for the problem, or another way of clearing the panel?

Thanks heaps,

Ash

PS: I'm using Visual Studio 2005
Oct 14 '07 #1
2 1705
Hi guys,

I have a panel on a form and at runtime I create some controls on the panel. Then, also during runtime I want to clear the panel of all of its controls. The basic way to do this is with the following code:

Expand|Select|Wrap|Line Numbers
  1. For Each this_ctrl As Object In target_panel.Controls
  2.      this_ctrl.Dispose()
  3. Next
What happens when I use this code is that only every second control is removed from the panel! Yet, when I replace the .Dispose() method with some other behaviour (like change the control's colour) it appears that every control is accessed correctly. Repeating the code block iteratively removes every second control until none are left, but this is hardly an elegant way to proceed.

So I think this is strange. Can anyone suggest a cause for the problem, or another way of clearing the panel?

Thanks heaps,

Ash

PS: I'm using Visual Studio 2005

See it is not strange,you iterating the Collection object and remove the controls
from it so the for loop wont iterate for the number of controls you have in the panel.Instead of Dispose you try to make controls invisible,the for loop will work properly as you are not deleting the controls from the ControlCollection.
Hope it makes sense.

try to use
panel.Controls.Clear();

Regards
Jignesh
Oct 15 '07 #2
Brilliant!!! The .Clear() method works perfectly.

In general though, I didn't realise the limitations of "For Eaching" through an ArrayList.

What if I had a general ArrayList and wanted to remove some elements from it (those that match some criteria) by "For Eaching" through the list. Then by disposing those members that I do not want will I not have the same problem?

What is the correct way to iterate through and remove some members from an ArrayList?

Thanks,

Ash
Oct 15 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: julio | last post by:
I noticed that pressing spacebar when a listbox has the focus scrolls the listbox to the first selected item. I'm intercepting the spacebar press event at form level for stopping and starting...
4
by: Wayne Aprato | last post by:
I have a simple database which was originally written in Access 97. When converted to Access 2000 file format it ran flawlessly in Access 2002. I've just tried to run it in Access 2003 and I am...
1
by: Ian | last post by:
Hi, I have noticed some strange behaviour when loading my database. I load a menu form on startup; this form contains a linked image to display. If the project is not then signed, the menu form...
0
by: Dot net work | last post by:
Hi, Make up a very simple project as follows: 1 aspx form 3 web user controls (referred to as A, B, and C) "A" web user control: Put 1 textbox and 1 button on this web user control. (You...
29
by: Charles Law | last post by:
Further to my issue about user controls, I have a problem with DesignMode. Here is the project hierarchy: MainApp |_ Project1 |_ SubProject (UserControl) SubProject has a default constructor...
8
by: Spam Trap | last post by:
I am getting strange resizing problems when using an inherited form. Controls are moving themselves seemingly randomly, but reproducibly. "frmBase" is my base class (a windows form), and...
5
by: Ian | last post by:
Hi everyone, I have found some bizarre (to me...!) behaviour of the Form_Activate function. I have a form which has a button control used to close the form and a subform with a datasheet view...
2
by: Hans Kesting | last post by:
Hi, Navigating with "tab" and "shift-tab" between textbox and radiobutton has sometimes some strange results. Some example fields: - several textboxes (say: tb1, tb2) - some radiobuttons...
6
by: Wayne | last post by:
I have a database that has been in use for a long period of time as an mde without giving any problems. I've just started making some changes to the main form which consists of a continuous form...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.