Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / October 2004

Tip: Looking for answers? Try searching our database.

using foreach to dispose controls causes exception

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Barry Anderberg - 29 Oct 2004 22:58 GMT
The following code snippet throws an exception:

public Form1()
{
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent();

    Control[] c = new Control[] { new Control(), new Control(), new
Control(),
    new Control(), new Control(), new Control(),
    new Control(), new Control(), new Control(),
    new Control(), new Control(), new Control(),
    new Control(), new Control(), new Control(),};

    Form form = new Form();
    form.Controls.AddRange( c );

    foreach( Control cont in form.Controls )
    {
        cont.Dispose();
    }
}

The reason is that the Control.Dispose() method actually removes
itself from the Parent control collection so half way through the
foreach loops the cont variable is null.

What is the normal way to itterate through a control collection
disposing of controls?

And yes, I realize that controls dispose of the controls in their
collection but I have an instance where I can't dispose of the parent
control but still need to dispose of the controls in its collection.
Ken Tucker [MVP] - 30 Oct 2004 04:41 GMT
Hi,

       Use a for loop instead.  Start with last control end with first

Control[] c = new Control[] { new Control(), new Control(), new

Control(),

new Control(), new Control(), new Control(),

new Control(), new Control(), new Control(),

new Control(), new Control(), new Control(),

new Control(), new Control(), new Control(),};

Form form = new Form();

form.Controls.AddRange( c );

for( int x = this.Controls.Count-1; x>=0; x-- )

{

this.Controls.RemoveAt(x);

}

Ken

------------------------

The following code snippet throws an exception:

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

Control[] c = new Control[] { new Control(), new Control(), new
Control(),
new Control(), new Control(), new Control(),
new Control(), new Control(), new Control(),
new Control(), new Control(), new Control(),
new Control(), new Control(), new Control(),};

Form form = new Form();
form.Controls.AddRange( c );

foreach( Control cont in form.Controls )
{
cont.Dispose();
}
}

The reason is that the Control.Dispose() method actually removes
itself from the Parent control collection so half way through the
foreach loops the cont variable is null.

What is the normal way to itterate through a control collection
disposing of controls?

And yes, I realize that controls dispose of the controls in their
collection but I have an instance where I can't dispose of the parent
control but still need to dispose of the controls in its collection.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.