How to Change the Cursor to a Wait Cursor in C#

By FoxLearn 12/12/2024 9:58:34 AM   80
To change the cursor to a wait cursor in a C# application, you can use the Cursor.Current property or Cursor class methods available in Windows Forms or WPF.

How to Display the Wait/Busy Cursor in C#?

In a Windows Forms application, you can change the cursor to a wait cursor using Cursor.Current or the UseWaitCursor property of a form or control.

For example, Using Cursor.Current

// Change the cursor to the wait cursor
Cursor.Current = Cursors.WaitCursor;
// Do something
// Change the cursor back to the default arrow
Cursor.Current = Cursors.Default;

The Cursor.Current property is used to globally change the cursor for the entire application or when you want to specifically change the cursor in certain parts of the application.

For example, Using UseWaitCursor

// Set the form's UseWaitCursor property to true
this.UseWaitCursor = true;
// Do something
// Set the form's UseWaitCursor property to false to restore the default cursor
this.UseWaitCursor = false;

The UseWaitCursor property is often used in Windows Forms because it automatically sets the cursor to the wait cursor when set to true for the form.

In a WPF application, you can set the cursor using the Mouse.OverrideCursor property.

// Change the cursor to the wait cursor
Mouse.OverrideCursor = Cursors.Wait;
// Do something
// Change the cursor back to the default arrow
Mouse.OverrideCursor = null; // Reset to the default cursor

The Mouse.OverrideCursor property allows you to set a custom cursor for the entire application. Setting it to Cursors.Wait will make the cursor appear as the 'wait' cursor.

By using the above methods, you can easily set the wait cursor while performing long-running tasks in your C# applications and improve the user experience by indicating that the application is busy.