Good day, I have a form with so much visual controls, so it takes a while to load all the window, I think it could be better if the form UI loads without showing it and when the UI loads all the controls, then show it, so the user won't see the controls loading.
-
It's usually rare that the UI elements take that long to load. It's usually the data you're loading to display in those controls. It's better to try to separate the UI from the data. Is there any chance you could post a [mcve] so that we could properly diagnose? Otherwise this isn't a very good question and should be deleted. – Enigmativity Jul 15 '18 at 03:34
-
If the problem is that there are too many controls, the solution should be rather obvious. – Ňɏssa Pøngjǣrdenlarp Jul 15 '18 at 04:30
-
@Plutonix Great to see you again. Every time I saw a DataGridView question, I would think of you. :-) – Mary Jul 15 '18 at 06:01
2 Answers
If the form is your startup form, consider using a splashscreen.
Otherwise, first try to find what control is taking time to load, maybe you are populating a list, fetching external datas... In that case, make those initialization asynchronuous.
If you just want to make the form invisible while loading, why don't you set the form visibility to hidden and show it in the form load event?

- 408
- 3
- 5
-
Yeah, I already have a splash screen that it should load my main form but that is the problem, I Also tried with Hide(); but it's the same, I need to load the content of the form to then open it so the user won't see the controls loading, just like another App – Sergio Marquez Jul 15 '18 at 02:51
-
And what if you call SuspendLayout() before InitializeComponents() and ResumeLayout() in Load Event ?? – Yves Israel Jul 15 '18 at 08:45
Usually its not the number of controls on your form that causes this, but loading them with values by a time consuming method while showing the form.
So you need to show your form fully and only then start loading data.
There are 2 ways you can do this.
- Call
Application.DoEvents():
in theFormShown
event - Call
This.Update();
in theActivated
event
For the second solution you need to declare a bool variable to use as a flag so you dont do this every time
private bool _firstTime = true;
private void Form1_Activated(object sender, EventArgs e)
{
if (_firstTime)
{
_firstTime = false;
this.Update();
FetchDataAndOtherTimeSpendingStuffNow();
}
}
In both cases, make sure you only start loading data and other time consuming code after this has been done.
That will make sure your form is shown with all controls on it and only then starts filling the controls with data.

- 11,359
- 6
- 44
- 79
-
-
are you saying that loading the form complete, but without any loading of data, is taking much longer when you force all controls to update immediate ? How many controls are on this form ? Are there any custom controls ? Are you sure no DB connection is done during showing ? – GuidoG Jul 17 '18 at 06:48
-
There are no data base connection, there are several controls and a user control – Sergio Marquez Jul 17 '18 at 15:26
-
is there any code in the load, the shown or activate on any place that could be executed while showing the controls ? – GuidoG Jul 17 '18 at 15:43
-
Yes, it's a code that comprobates a variable saved in user settings, if it's false, then, it changes Al the controls colors – Sergio Marquez Jul 17 '18 at 16:12
-
Try it with that code disabled. The problem could very well be in that code – GuidoG Jul 17 '18 at 22:37
-
Yeah I Have Tried It Without That Code, But I Really Need To Have It There – Sergio Marquez Jul 17 '18 at 22:38
-
does it works fast without that Code ? If so than show us that code maybe there can be optiimized there – GuidoG Jul 17 '18 at 22:39
-
It's a verte long code Just changing properties, like. private void DarkTheme(). {. picturebox1.image = //image;. picturebox1.BackColor = //Color;}. The problem here is that I don't have access Right now to my full code, but, the code is basically that, just change the image and backcolor for all the controls in my form, and for the labels, it changes the forecolor and backcolor – Sergio Marquez Jul 17 '18 at 22:43
-
Well there you have it. Im sure we found the cause of the delay. Load your forms with that code commented out and I am sure it will load fast again. Then you need to optimize this. Best you make a new question where you show that code and ask how to optimize it – GuidoG Jul 18 '18 at 06:37