What is the best practice for changing form content using UserControls in VB.NET?

I'm currently writing a small form based VB.NET application using VS2010.
The user interface consists of a left side outlook style menu and a content container.
This layout has been implemented using a SplitContainer, and the Panel2 og this is logically acting as the content container.

Using the menu, the user is able to change the content in the content container.
The content has been predesigned using UserControls.
Currently I'm doing this simply by using the following code:

With MainForm.SplitContainer.Panel2.Controls
   .Clear
   .Add(FormToShow)
   FormToShow.Dock = DockStyle.Fill
End With

Open in new window


Being new to VB.NET I did this as it basically worked out. However I have a feeling that its an in efficient way to do it. I'm sure its a very common situation, so what is the best practice for implementing this kind of functionality?
andreas_rafnAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nepaluzCommented:
I have similar situation and have opted for using the Visible flag of the control. To achieve this, you have to give each of your controls names AND add them all (once, at initialisation) to the panel; you then iterate through all the controls on the panel and set the visible flag to true on the name you want, and false on the rest.

I found this enabled me to call whatever user control I wanted easier as I could name them as their menu items.
0
Jacques Bourgeois (James Burger)PresidentCommented:
You have to balance between your solution and the one suggested by Nepaluz. It depends on the application.

Your's is more efficient if users typically use only a few of the possible UserControls during the life of the application, because you do not use up resources that will never be called.

Nepaluz is best if they will typically use almost all of the controls, because the application will probably be more responsive for the user. He will also be able to come back to a previously opened control with the same data displayed if necessary.

Even better is mine :-) , a mix of both. This is what I use most often in that type of interface.

Have a series of variables defined but not initialized for each of your UserControl. When one is required, have code like the following.

YourCurrentlyActiveControl.Visible=False
If YourNewControl Is Nothing then
    YourNewControl = New YourControl
    Clear
    Add
    Dock
Else
   YourNewControl.Visible=True
End if

That way, the application will start faster because it does not have to create all the controls on launch. You use ressources only if required by the user. But if the user tends to stick to the same type of information during the life of the application, the ones he already used are awaiting him and will just pop up very fast when set to Visible.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
andreas_rafnAuthor Commented:
I'll go for the combined method, seems like a nice and logical way to do it.
Most likely I will preload a small amount of forms that will almost certainly be used, and load the ones less often used on demand.
Thanks for the advice.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.