C# WaitCursor while form loads

Alexandre Bell picture Alexandre Bell · Nov 30, 2009 · Viewed 25.8k times · Source

I'm having a form that takes a few seconds to finally display. This form is called through:

using (ResultsForm frm = new ResultsForm())
{
    this.Visible = false;
    frm.ShowDialog();
    this.Visible = true;
}

It's useful that I get the default cursor to Cursors.WaitCursor while waiting for the form to finally display. Currently I can only seem to be able to do this successfully by using the static 'Current' property:

using (ResultsForm frm = new ResultsForm())
{
    //this.Visible = false;
    Cursor.Current = Cursors.WaitCursor;
    frm.ShowDialog();
    //this.Visible = true;
}

But this has two problems:

  • It forces me to disable the MainForm hiding feature which I would like to retain.
  • It increases coupling since Cursor.Current = Cursor.Default; needs to be called within the ResultsForm Shown event.

How can I change the Cursor while the form loads without changing the first code snippet and while avoiding coupling?

UPDATE: Now the question was answered, video presentation was removed so I don't go over my ISP bandwidth limitations.

Answer

Reed Copsey picture Reed Copsey · Nov 30, 2009

It forces me to disable the MainForm hiding feature which I would like to retain.

You should be able to do both, without issues.

It increases coupling since Cursor.Current = Cursor.Default; needs to be called within the ResultsForm Shown event

Have you tried putting your cursor logic entirely into the ResultsForm dialog code? You should be able to set Cursor.Current = Cursors.WaitCursor; inside of the ResultsForm's constructor, and then have Cursor.Current = Cursor.Default; set inside of the Shown event.

This would keep the logic entirely in the dialog, and out of the main window. You could also then keep the visibility toggling in the main window.