Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Derik Whittaker

Thoughts on Software Development, .Net, OOP, Design Patterns and all things cool



Howto: Manage Cursor Changing for WinForms Applications

As a long time WinForms developer I always have had the need to swap out the current cursor (mouse pointer, not some database cursor) for another cursor, normally the ‘wait’ cursor, while performing some task after things such as a button click.

In the past I have used some pretty straight forward code that solved this problem easily, but was a pain to replicate because of the number of lines of code.  The good news was that when .Net 2.0 came out and they included the ‘using’ keyword in VB.net I found a much simpler and elegant solution to my problem.
 
Here is the way that I once swapped out cursors: 

            // Grab the current, entry cursor

            // Set the current cursor to the wait and continue

            Cursor currentCursor = Cursor.Current;

            Cursor.Current = Cursors.WaitCursor;

            try

            {

                // Do something here

            }

            finally

            {

                // Swap the current cursor back to the original cursor

                Cursor.Current = currentCursor;

            }


Here is the new 'more elegant' way by using the 'using' keyword:

      using ( new CursorKeeper( Cursors.WaitCursor ) )

      {

       // Do something here

      }

As you can see, by using the 'using' keyword I have reduced the number of lines needed to be repeated to only a few.

Now, lets take a look at the code in the CursorKeeper class to see how this works behind the scenes.

    public class CursorKeeper : IDisposable

    {

        private Cursor _originalCursor;

        private bool _isDisposed = false;

 

        public CursorKeeper( Cursor newCursor )

        {

            _originalCursor = Cursor.Current;

            Cursor.Current = newCursor;

        }

 

        #region " IDisposable Support "

        protected virtual void Dispose( bool disposing )

        {

            if ( !_isDisposed )

            {

                if ( disposing )

                {

                    Cursor.Current = _originalCursor;

                }

            }

            _isDisposed = true;

 

        }

 

        public void Dispose()

        {

            // Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.

            Dispose( true );

            GC.SuppressFinalize( this );

        }

 

        #endregion

    }

 

 

Well, any I thought I would share how we manage the cursor changes with everyone.  If someone else has another way to handle this, let me.

 

Thanks and happy developing..... DW



Comments

Steve said:

This is awesome!  It takes care of going back to the default cursor too soon by holding the current cursor in memory, allowing nested calls that change the cursor.

# October 6, 2006 8:48 AM

Daniel Arak said:

Pretty nice -> instead of using code snippet you propose very simple and in the same usefull solution. Great Derik!

# October 8, 2006 7:03 AM

Rob Eisenberg said:

Great way to leverage the using keyword!

# October 8, 2006 1:06 PM

jokiz said:

i still have a perception that windows apps manipulating these cursors are doing way to much on the UI thread that makes it unresponsive, better use worker threads for this.

# October 12, 2006 10:37 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Derik Whittaker

Derik is a .Net Developer/Architect specializing in WinForms working out the northern suburbs of Chicago. He is also believer and advocate for Agile development including SCRUM, TDD, CI, etc.

When Derik is not writing code he can be found spending time with his wife and young son, climbing on his bouldering wall, watching sports (mostly baseball), and generally vegging out. Check out Devlicio.us!

Our Sponsors

Proudly Partnered With


This Blog

Syndication

News