jump to navigation

CWaitCursor for WinForms September 1, 2006

Posted by codinglifestyle in C#, Winform.
Tags: , , ,
trackback

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))

    {

        

    }

Advertisements

Comments»

No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: