0

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.

enter image description here

^ For example: Clicking the button Settings will open FormSettingsinside 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.

MadsTheMan
  • 705
  • 1
  • 10
  • 32
  • 1
    Usually just opening forms should not take long unless those forms are either very very complex or performing some blocking actions during the constructor. If the latter is the case, consider de-coupling the initialization from the constructor and run in on a background thread, otherwise the GUI thread has to do the work and that prevents a lot of visual updates. – Adwaenyth Oct 02 '18 at 11:40
  • 1
    What stops you from preloading all the forms during the load of the application? `btnSettings_Click` doesn't set any values for the form it is opening, so is there any other part of the application that would change how the opened form would look? If this is not the case, you could prepare all the forms and just use the `Show` and `Hide` methods when a button is clicked. – Markus Deibel Oct 02 '18 at 11:50
  • Look at my two form project : https://stackoverflow.com/questions/34975508/reach-control-from-another-page-asp-net – jdweng Oct 02 '18 at 12:07
  • 1
    Inefficient code tends to be easy to overlook. But less so in Winforms, you can *see* it. If you know these controls need to load of lot of data before they can't paint themselves then you don't need a profiler yet. Flicker elimination hints in [this post](https://stackoverflow.com/questions/2612487/how-to-fix-the-flickering-in-user-controls/2613272#2613272). – Hans Passant Oct 02 '18 at 12:23
  • @Adwaenyth I'll look into that. However, I'm not very experienced with coding so if it's fairly technical I guess I'll have to just be a bit less critical of the application and run with the simple version that is right now - or something similar. I'll definitely give it try tho. I guess, in the end, my question is pretty much about figuring out whats normal to do here. – MadsTheMan Oct 02 '18 at 12:27
  • @MarkusDeibel Good question! I can do some tests and see if that works a lot better for me. Might do! – MadsTheMan Oct 02 '18 at 12:28
  • @HansPassant Exactly! I didn't even know flicker elimination in that sence was possible - this might potentially solve my question in many ways, because it is in the end about making a smoother load of controls/form. I'll look into it and try out the things that I can try through the post / the answer you have there, or at least suck up some knownledge. – MadsTheMan Oct 02 '18 at 12:35
  • Use usercontrols instead forms. – Luis Oct 02 '18 at 13:18

0 Answers0