WinForm: Removing Controls from a WinForm
Messages   Related Types
This message was discovered on ASPFriends.com 'winforms-cs' list.


Robert Merriott
-- Moved from [aspngfreeforall] to [winforms-cs] by Alex Lowe <Click here to reveal e-mail address> --

Hello

Currently trying to remove textbox and label controls from a winform
Panel using: Form1.Controls.Clear(); (Form1 = the Content Panel)

This works to remove the controls and allow me to re-create new ones,
but unfortunately this makes the 'x' close button on the window not
operate properly. (Need to Ctrl-Alt-delete) my app.

I use the same code to clear all controls off the main WinForm and
everything works properly. This only fails when I try to do this when I
try to clear all components off a Panel Control.

Any suggestions would be very much appreciated, since I an not sure how
to solve this.

Thank you,

Robert Merriott

Reply to this message...
 
    
Ryan Trudelle-Schwarz
This is a known bug I believe. IIRC you can give the focus to a control =
that is not going to be removed and things will work properly.

-> -----Original Message-----
-> From: Robert Merriott [mailto:Click here to reveal e-mail address]
->=20
-> Hello
->=20
-> Currently trying to remove textbox and label controls from a winform
-> Panel using: Form1.Controls.Clear(); (Form1 =3D the Content Panel)
->=20
-> This works to remove the controls and allow me to re-create new ones,
-> but unfortunately this makes the 'x' close button on the window not
-> operate properly. (Need to Ctrl-Alt-delete) my app.
->=20
-> I use the same code to clear all controls off the main WinForm and
-> everything works properly. This only fails when I try to do this when =
I
-> try to clear all components off a Panel Control.
->=20
-> Any suggestions would be very much appreciated, since I an not sure =
how
-> to solve this.
->=20
-> Thank you,
->=20
-> Robert Merriott

---
[This E-mail scanned for viruses by Declude Virus]

Reply to this message...
 
    
Erik Brown
Yes, this is known bug and the recommended fix is to make sure none of
the to-be-removed controls has focus.

The way I usually fix this is to loop through the controls collection
and dispose of each one individually, which as a side affect also
ensures they do not have focus:

    foreach (Control c in Form1.Controls)
    {
        c.Dispose();
    }
    Form1.Controls.Clear();

Erik Brown
==========
Author of "Windows Forms Programming with C#"
www.manning.com/eebrown

[Original message clipped]

Reply to this message...
 
 




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com