I have a form that is very slow because there are many controls placed on the form.
As a result the form takes a long time to loaded.
How do I load the fo
You should create a background thread to to create and populate the form. This will allow your foreground thread to show the loading message.
You can take a look at my splash screen implementation: C# winforms startup (Splash) form not hiding
I put some animated gif in a form called FormWait
and then I called it as:
// show the form
new Thread(() => new FormWait().ShowDialog()).Start();
// do the heavy stuff here
// get the form reference back and close it
FormWait f = new FormWait();
f = (FormWait)Application.OpenForms["FormWait"];
f.Close();
Well i do it some thing like this.
NormalWaitDialog/*your wait form*/ _frmWaitDialog = null;
//Btn Load Click Event
_frmWaitDialog = new NormalWaitDialog();
_frmWaitDialog.Shown += async (s, ee) =>
{
await Task.Run(() =>
{
// DO YOUR STUFF HERE
//Made long running loop to imitate lengthy process
int x = 0;
for (int i = 0; i < int.MaxValue; i++)
{
x += i;
}
}).ConfigureAwait(true);
_frmWaitDialog.Close();
};
_frmWaitDialog.ShowDialog(this);
You want to look into 'Splash' Screens.
Display another 'Splash' form and wait until the processing is done.
Here is an example on how to do it.
Another way of making "Loading screen" only display at certain time is, put it before the event and dismiss it after event finished doing it's job.
For example: you want to display a loading form for an event of saving result as MS Excel file and dismiss it after finished processing, do as follows:
LoadingWindow loadingWindow = new LoadingWindow();
try
{
loadingWindow.Show();
this.exportToExcelfile();
loadingWindow.Close();
}
catch (Exception ex)
{
MessageBox.Show("Exception EXPORT: " + ex.Message);
}
Or you can put loadingWindow.Close()
inside finally
block.