Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2012-03-18
3
Medium Priority
?
312 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 800 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 1200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
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 …

926 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