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?
 
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
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.