Solved

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

Posted on 2012-03-18
3
308 Views
Last Modified: 2012-03-20
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?
0
Comment
Question by:andreas_rafn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 17

Assisted Solution

by:nepaluz
nepaluz earned 200 total points
ID: 37735104
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
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 300 total points
ID: 37738467
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
 

Author Closing Comment

by:andreas_rafn
ID: 37744150
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

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question