Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I show a "Loading . . . please wait" message in Winforms for a long loading form?

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 form first, then display it and while loading delay show another form which that have message like "Loading... please wait.?"

like image 789
Sadegh Avatar asked Dec 16 '09 22:12

Sadegh


People also ask

How use progress bar in C# Windows form?

To create a ProgressBar control at design-time, you simply drag a ProgressBar control from the Toolbox and drop onto a Form in Visual Studio. After you the drag and drop, a ProgressBar is created on the Form; for example the ProgressBar1 is added to the form and looks as in Figure 1.

Which event gets triggered when form gets loaded on screen?

The load event is called once all the components of the form are loaded. If you redisplay the form, its components load again and therefore the Load event is triggered once more.

How do I fix winform size?

Right click on your form and go to properties. Then go to Layout options,see there are a property named Size. Change it as your need as width and length wise.

How do I load a Windows Form?

Loader is an operating system that is used for loading Programs and Libraries. It is one of the essential stages in the process of starting a program. Click New >> Project >> Visual C# >> Windows >> Windows Forms Application. Enter your Project name and click OK.


4 Answers

Using a separate thread to display a simple please wait message is overkill especially if you don't have much experience with threading.

A much simpler approach is to create a "Please wait" form and display it as a mode-less window just before the slow loading form. Once the main form has finished loading, hide the please wait form.

In this way you are using just the one main UI thread to firstly display the please wait form and then load your main form.

The only limitation to this approach is that your please wait form cannot be animated (such as a animated GIF) because the thread is busy loading your main form.

PleaseWaitForm pleaseWait=new PleaseWaitForm ();  // Display form modelessly pleaseWait.Show();  //  ALlow main UI thread to properly display please wait form. Application.DoEvents();  // Show or load the main form. mainForm.ShowDialog(); 
like image 127
Ash Avatar answered Sep 23 '22 00:09

Ash


I looked at most the solutions posted, but came across a different one that I prefer. It's simple, doesn't use threads, and works for what I want it to.

http://weblogs.asp.net/kennykerr/archive/2004/11/26/where-is-form-s-loaded-event.aspx

I added to the solution in the article and moved the code into a base class that all my forms inherit from. Now I just call one function: ShowWaitForm() during the frm_load() event of any form that needs a wait dialogue box while the form is loading. Here's the code:

public class MyFormBase : System.Windows.Forms.Form {     private MyWaitForm _waitForm;      protected void ShowWaitForm(string message)     {         // don't display more than one wait form at a time         if (_waitForm != null && !_waitForm.IsDisposed)          {             return;         }          _waitForm = new MyWaitForm();         _waitForm.SetMessage(message); // "Loading data. Please wait..."         _waitForm.TopMost = true;         _waitForm.StartPosition = FormStartPosition.CenterScreen;         _waitForm.Show();         _waitForm.Refresh();          // force the wait window to display for at least 700ms so it doesn't just flash on the screen         System.Threading.Thread.Sleep(700);                  Application.Idle += OnLoaded;     }      private void OnLoaded(object sender, EventArgs e)     {         Application.Idle -= OnLoaded;         _waitForm.Close();     } } 

MyWaitForm is the name of a form you create to look like a wait dialogue. I added a SetMessage() function to customize the text on the wait form.

like image 20
goku_da_master Avatar answered Sep 20 '22 00:09

goku_da_master


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.

like image 32
David Basarab Avatar answered Sep 23 '22 00:09

David Basarab


A simple solution:

using (Form2 f2 = new Form2())
{
    f2.Show();
    f2.Update();

    System.Threading.Thread.Sleep(2500);
} // f2 is closed and disposed here

And then substitute your Loading for the Sleep.
This blocks the UI thread, on purpose.

like image 28
Henk Holterman Avatar answered Sep 23 '22 00:09

Henk Holterman