Click here to Skip to main content
15,790,807 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using Microsoft Visual Studio creating a Windows Form Application. **I don't know how I should add my User Controls to my Form.** I have seen many examples on how you can do it, but I am yet to find an answer that takes performance into account, atleast that's what I believe.

Here are the two methods that I have used:

1. **UserControl.Visible = True / False**

One of the more common methods that I have seen is to place your User Control in your Form and setting the visibility to False. Whenever the User Control needs to be visible you simply set Visible to True. I personally feel that this is a very bad way to handling User Controls. This can become very disorienting when you have a lot of controls in your Form. *This is the method I was taught to use.*

The code can look something like this:

' User Control is already placed in the Form and Visibility is set to False
    Public Class Form_Main

        ' Button to make User Control Visible
        Private Sub Button_Visible_Click(sender As Object, e As EventArgs) Handles Button_Visible.Click
            UserControl_1.Visible = True
        End Sub

    End Class

2. **Load User Control "Dynamically"**
Loading a User Control dynamically implies that I add the User Control to a container (Panel, etc.)

The code can look something like this:

Public Class Form_Main

        ' Instance User Control
        Public Shared UC_Main As New UserControl_Main
        ' On Form Load
        Private Sub Form_Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ' Add Main Menu Control
        End Sub

    End Class

**The question about Performance and Method**

What methods of adding and disposing User Controls are most efficient in terms of performance?

What I have tried:

I have tried both of the alternatives above, but are still confused on how I should do this.
Updated 17-Nov-17 3:23am

1 solution

There bis no generell way to answer your question.
Basicly :
A Control which is part of your Form OR part of a Control on your Form (like Panel) belongs finally to the Form (the Panel belongs to the Form and the Control belongs to the Panel).
So there is no difference in efficiency .. except : a Panel is a ContainerControl which could do common functionality for you (if you switch the Panel to Visible=False then all included Controls become also invisible. The same Behaviour has your UserControl (because it's also a ContainerControl).

If you need a Control on your Form you should place it there. If you need the Control depending on a state you make it visible or invisible (with it's Property) or usable with Enabled.
A dynamicly created Control is a good choice if you want to have (for example) Controls depending on different settings (an input into a Textbox). Those Controls will be instanced and added to the Controls-Collection of the Container when needed (and not generally placed).

To give you an advice (for me) it's necessary to have more information about your issue ...
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900