Solved

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

Posted on 2012-03-18
3
303 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
3 Comments
 
LVL 17

Assisted Solution

by:nepaluz
nepaluz earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now