Confused obout setting a Cursor

  • Thread starter Thread starter Academic
  • Start date Start date
A

Academic

What are the different effects of the following two statements:

C1.Cursor = Cursors.WaitCursor

C1.Cursor.Current = Cursors.WaitCursor



I believe the first replaces the entire C1.Cursor object with a new one
while the second only replaces the Current object of C1.Cursor, but I can't
figure when to use which.



Thanks for any help!
 
Hi,

If C1 is an instance of a Control:
C1.Cursor = Cursors.WaitCursor

This will set the cursor to be the Window's WaitCursor (usually an hour glass)
when the mouse is within the boundaries of the Control and any of its children
C1.Cursor.Current = Cursors.WaitCursor

This will not compile in C#. Cursor.Current is a static property and cannot
be accessed through an instance of a Cursor object. When set, the current
cursor being displayed is supposedly changed, according to the MSDN docs, but
it seems to be reverted back to Cursors.Default immediately in my testing.
 
If you're in some method that will take a while do you set Cursor or
Current?


Just before you leave the method do you reset to Default or is that
automatic?


Thanks for the help


Dave Sexton said:
Hi,

If C1 is an instance of a Control:


This will set the cursor to be the Window's WaitCursor (usually an hour
glass) when the mouse is within the boundaries of the Control and any of
its children


This will not compile in C#. Cursor.Current is a static property and
cannot


bad example. should be

System.Windows.Forms.Cursor.Current

be accessed through an instance of a Cursor object. When set, the current
cursor being displayed is supposedly changed, according to the MSDN docs,
but it seems to be reverted back to Cursors.Default immediately in my
testing.


Before leaving the routine the setting is in?
 
Hi,
If you're in some method that will take a while do you set Cursor or
Current?

Setting Cursor.Current doesn't seem to do anything so I wouldn't recommend
using it at all.

If you're going to be processing some long-running task then you should be
doing that on a thread other than the UI thread (see BackgroundWorker class).

If you want to display a WaitCursor for every Form in your application then
set Application.UseWaitCursor = true, or you can set UseWaitCursor on any
particular Control, including Forms.

In many cases you must also disable parts of the UI in code as well as setting
the WaitCursor. e.g., Control.Enabled = false and Form.Enabled = false.
Just before you leave the method do you reset to Default or is that
automatic?

In the "RunWorkerCompleted" event handler for the BackgroundWorker class you
should reset the cursor to Cursors.Default or just set UseWaitCursor = false
(the simpler of the two choices but it only works when you originally set
UseWaitCursor = true).
 
Dave Sexton said:
Hi,


Setting Cursor.Current doesn't seem to do anything so I wouldn't recommend
using it at all.

If you're going to be processing some long-running task then you should be
doing that on a thread other than the UI thread (see BackgroundWorker
class).

If you want to display a WaitCursor for every Form in your application
then set Application.UseWaitCursor = true, or you can set UseWaitCursor on
any particular Control, including Forms.


I had just found the Application.UseWaitCursor in Help.
It said what happens when you set it to true (all open form get set to true)
but not what happens when you set it to false.

I'm thinking about nested routines all setting it to true. Then the
innermost routine sets it to false.
Since it is boolean I'd guess the cursor gets reset on all open forms even
tho some routines are not finished.
and that this is something one must make sure does not happen.
Correct?

Actually as I think about it one has the same problem setting Forms.Cursor.
Correct?
 
Hi,

I'm thinking about nested routines all setting it to true. Then the
innermost routine sets it to false.

I prefer that the UI thread handles the cursor logic and the "routines" just
process the business logic. Therefore, it's more common that the "outer"
routine sets the UseWaitCursor property to true before any of the asynchronous
processes are started and false after the entire operation is completed,
including all sub-routines that are being processed asynchronously. This, of
course, only applies to cases where you really can't allow the user to
interact with the all or part of the UI while the processing is taking place.
In some cases unrestricted user-interaction isn't a problem.
Since it is boolean I'd guess the cursor gets reset on all open forms even
tho some routines are not finished.

When UseWaitCursor is set to true before all of the operations are complete,
yes. Try it yourself to verify.
and that this is something one must make sure does not happen.
Correct?

If you allow sub-rountines to control the cursor logic then yes, you may run
into problems when one process sets UseWaitCursor to false while another
asynchronous process hasn't completed yet. You can prevent this by processing
all UI-related logic on the UI thread only, as I suggested above.
Actually as I think about it one has the same problem setting Forms.Cursor.
Correct?

Yes.
 
Hi,

Correction:

When UseWaitCursor is set to FALSE before all of the operations are complete,
yes. Try it yourself to verify.
 
Thanks for taking so much time to explain.



Dave Sexton said:
Hi,

Correction:

When UseWaitCursor is set to FALSE before all of the operations are
complete, yes. Try it yourself to verify.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top