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

Dynamically remove child controls ?

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 GroupBox1.Controls
If CStr(ctl.Tag) = "Removable" Then
GroupBox1.Remove(ctl)
End If
Next

After much debugging, I realized that when a Control is removed from the
control collection, all
subsequent controls are moved up one position in the collection.

For instance, if I have 4 such removable controls, then this code results in
only 2 being removed.
(The first and the 3rd in the collection.) I have understood why this is
happening, and could draw a
diagram to explain it, but I can't think of a workaround, other than
hard-coding the index nos. of
the controls. I need to remove all 4 of the controls.

Basically, what happens is that when ChildControl(0) is removed, the
remaining 3 controls get moved up in index no. So, ChildControl(1) now
becomes ChildControl(0). My loop moves to the next control (ChildControl(1))
and deletes the 3rd control in original collection. It gives an error in the
next round, since ChildControl(2) does not exist anymore.

There *must* be a pretty way to do this !

Any help would be appreciated.

Regards,

Cerebrus.

Feb 10 '06 #1
2 1599
Cerebrus,

Because of this problem I use for removing from a collection, even if it is
not selective, the method to do it bottomwards up.

\\\
For i as integer = GroupBox1.Controls.Count - 1 to 0 step -1
dim ctl as Control = GroupBox1.Controls(i)
If CStr(ctl.Tag) = "Removable" Then
GroupBox1.Remove(ctl)
End If
Next
///
typed here so watch typos or whatever.

I hope this helps,

Cor
Feb 10 '06 #2
Hi Cor,

LOL ! Trust me to miss something so obvious ! Thanks a lot ! It works now.

Regards,
Cerebrus.

Feb 10 '06 #3

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

Similar topics

8
by: Donald Xie | last post by:
Hi, I noticed an interesting effect when working with controls that are dynamically loaded. For instance, on a web form with a PlaceHolder control named ImageHolder, I dynamically add an image...
0
by: Earl Teigrob | last post by:
I am building a custom control that I want to server as a container for child controls that can be dynamically added to this control. I can persist the child controls that are added to my custom...
4
by: Harry | last post by:
Hello, I have a page with a RadioButtonList and a PlaceHolder control. The RadioButtonList's AutoPostBack attribute is set to TRUE and its SelectedIndexChanged event loads one of three...
2
by: Chad | last post by:
I have a problem that I am desperate to understand. It involves dynamically adding controls to a Table control that is built as a result of performing a database query. I am not looking to...
5
by: Dennis Fazekas | last post by:
Greetings, I am creating a web form which will all the user to add an unlimited number of email addresses. Basically I have 3 buttons, "Add Another Email", "-" to remove, and a "Save" button....
5
by: Amoril | last post by:
I've read quite a few different message on various boards and for some reason I'm still having trouble wrapping my head around this viewstate maintenance and trying to get these dynamically created...
5
by: Andrew Robinson | last post by:
I have a page that can load a number of different user controls. Each of these user controls inherits from a common base class and the controls are loaded based on application state, status, etc...
1
by: semomaniz | last post by:
I have a form where i have created the form dynamically. First i manually added a panel control to the web page. Then i added another panel dynamically and inside this panel i created tables. I have...
29
by: shivasusan | last post by:
Hi! I can add rows with inputs to my HTML table dynamically using DOM, but I cannot remove selected rows. In fact, every row contains a Delete button. So, user selects the rows to remove, clicks...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.