jump to navigation

CWaitCursor for WinForms September 1, 2006

Posted by codinglifestyle in C#, Winform.
Tags: , , ,
add a comment

There is no doubt, developing Windows applications in .NET is miles easier than in the bad old days of MFC.  However, there are times when MFC got it right and I found one of them this week.  MFC had a handy class called CWaitCursor.  You simply instantiated a local variable which would change the cursor to the wait icon.  When the local variable went out of scope, the cursor changed back.  I couldn’t find this functionality in .NET, but it was simple enough to duplicate:

    internal class CWaitCursor : IDisposable

    {

        private Form m_Form;

 

        public CWaitCursor(Form form)

        {

            m_Form = form;

            m_Form.Cursor = Cursors.WaitCursor;

        }

 

        public void Dispose()

        {

            m_Form.Cursor = Cursors.Default;

        }

    }

 

To use in your form create a local instance and pass the this pointer.  The using can be used to guarantee an immediate call to Dispose() like so:

    using (new CWaitCursor(this))

    {

        

    }