I am using Microsoft Visual Studio creating a Windows Form Application (Small Puzzle Game). 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:
- 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.
- 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 Panel_Container.Controls.Add(UC_Main) 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?
Here is an example of what I am trying to do and my issue:
I have an option menu with 5 buttons, all of which adds a new User Control to the form (Using method 2). I use the Control.ControlCollection.Remove
Method to remove a control whenever want to go back to the options menu again. Whenever I then start the game I dispose all unnecessary User Controls to save performance. The problem is that I have to re-instance all User Controls whenever I want the user to go back to the options menu.
I am aware that windows forms are not created for Game-Development in any way.