Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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


Posted 10-06-2006 6:26 AM by Derik Whittaker
Filed under: , ,

[Advertisement]

Comments

Steve wrote re: How to simply Manage Cursor Changing for WinForms Applications
on 10-06-2006 8:48 AM

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.

Daniel Arak wrote re: How to simply Manage Cursor Changing for WinForms Applications
on 10-08-2006 7:03 AM

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

Rob Eisenberg wrote re: How to simply Manage Cursor Changing for WinForms Applications
on 10-08-2006 1:06 PM

Great way to leverage the using keyword!

jokiz wrote re: Howto: Manage Cursor Changing for WinForms Applications
on 10-12-2006 10:37 AM

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.

temasparapian wrote re: Howto: Manage Cursor Changing for WinForms Applications
on 05-03-2009 9:29 PM

birthday gift

relishjos wrote re: Howto: Manage Cursor Changing for WinForms Applications
on 05-06-2009 3:52 AM

birthday gift

pimpinnevaslipi wrote re: Howto: Manage Cursor Changing for WinForms Applications
on 05-09-2009 5:30 AM

best apartment design

BRQrpnZuIumWxdx wrote re: Howto: Manage Cursor Changing for WinForms Applications
on 10-11-2009 9:10 AM

awegaw1.txt;2;5

SouthWind29 wrote re: Howto: Manage Cursor Changing for WinForms Applications
on 10-23-2009 7:01 AM

I'm averaging quite a few views each day so I would like my readers to be able to find me on my new blog. ,

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)