Windows Forms - UserControls - Minimising problem

SlammingRumJoe

Newcomer
Joined
Jan 25, 2005
Messages
1
Guys,

I have created a simple form with a large panel in the middle. Instead of using multiple forms i thought that i would use one form with a panel which contains a UserControl. Instead of hiding one form and showing another (i couldnt get them to appear in exactly the same location!) I was clearing the controls collection of the panel and adding a new usercontrol to it.

The only problem is that i am clearing the controls collection of the panel from a control in the panel. Is this possible?

Here is the code that i was using:
Dim asd As frmMain ' the form on which the main panel - pnlMainPage - resides
'
asd = Me.ParentForm
asd.ClearMainPanel()
asd.pnlMainPage.Controls.Clear()
'
Dim ucPers As New ucPersonalInfo ' new usercontrol
'
asd.pnlMainPage.Controls.Add(ucPers)
ucPers = Nothing
'
asd = Nothing

This works but everytime the application is minimised or loses focus after this it is impossible to get it back into focus. What am i doing wrong? I am a Web Developer and am havnig much trouble adapting to this windows forms thing.

Help!
 
Joe:
I use the technique you have described of having a Panel in a Form which serves as a container for swapping out User Controls quite often. I always manipulate the Controls[] collection of the Panel from the parent Form and have never run into any problems. An easy way to approach this is to instantiate the User Control and register an event handler from the User Control to respond to in the Form prior to adding it to the Panel's Controls[] collection. You can then cleanly remove and dispose of the User Control without causing any conflicts.
 
Back
Top