I'll simplify my example from the my actual project a bit for the sake of explaining everything, making it easier to understand. I have a mainform with menubuttons on the far left and a panel in the middle. Clicking any of the menubuttons, will open a form within PanelForms
, and close the previously opened. See my illustration below.
^ For example: Clicking the button Settings
will open FormSettings
inside PanelForms
.
What is a good way of doing this / best practice? In the code under you'll see what I'm doing, but I feel like it could do with some optimization - and doing it this way will not allow me to add preloaders (A simple GIF or progressbar would do just fine) upon loading the form onto the panel. I'm assuming the solution is working with threading
and/or backgroundworkers
, but I can't find any good solution that covers my situation - or figure out how to use it correctly.
Right now, this is what I do (exact same for the other forms):
formSettings fsOpen = null;
private void btnSettings_Click(object sender, EventArgs e)
{
fsOpen = new formSettings();
fsOpen.TopLevel = false;
fsOpen.AutoScroll = true;
fsOpen.AutoScaleMode = AutoScaleMode.Dpi;
this.panelForms.Controls.Add(fsOpen);
fsOpen.Dock = DockStyle.Fill;
fsOpen.Show();
fsOpen.BringToFront();
}
Doing it this way let me close the form easily and replace it with another form when I click another menuButton like so fsOpen.Close();
.
I'm not expecting a full on detailed answer that completely solves this, but if someone at least can put me in the right direction (preferably with examples), that would be great. The information I found around googling was very confusing to me - and it seems to be many ways to go about this.
Additional info
It's a .net windows application made with Visual Studio 2017 - using both code and the designer tool. All the forms have a fair amount of controls to load, and a couple of them are reading quite a lot of information from XML and filling into datagridview for example. The forms take 1-2 seconds to load right now, which is not too bad, but I can see some flickering as it loads the controls on the forms and such.