How to Change Cursor in C#

Muhammad Zeeshan Feb 02, 2024
How to Change Cursor in C#

This article will guide you on changing the cursor into the wait cursor using the programming language C#.

Change Cursor to Wait Cursor in C#

When you move a mouse or trackball, Windows displays an icon called a cursor. Typically, various cursor images are shown depending on the program’s current state.

The standard and the wait cursors are two examples. Various operating systems can use different cursors.

Below is an example that will help you better understand the WaitCursor function.

We can give the value Cursors.WaitCursor to display the hourglass cursor.

Cursor.Current = Cursors.WaitCursor;

The functionality of our code may be written after the cursor has been assigned to the .WaitCursor variable. As an example, we’ve implemented a for loop.

try {
  Console.WriteLine("Here you will write your workings or tasks");
  for (int i = 0; i < 2; i++) {
    Thread.Sleep(1000);
  }
}

When we finish our operation, we will reset the cursor’s default setting, Cursor.Default.

finally {
  Cursor.Current = Cursors.Default;
}

Complete Source Code:

public void SampleTask() {
  Cursor.Current = Cursors.WaitCursor;
  try {
    Console.WriteLine("Here you will write your workings or tasks");
    for (int i = 0; i < 2; i++) {
      Thread.Sleep(1000);
    }
  } finally {
    Cursor.Current = Cursors.Default;
  }
}
Muhammad Zeeshan avatar Muhammad Zeeshan avatar

I have been working as a Flutter app developer for a year now. Firebase and SQLite have been crucial in the development of my android apps. I have experience with C#, Windows Form Based C#, C, Java, PHP on WampServer, and HTML/CSS on MYSQL, and I have authored articles on their theory and issue solving. I'm a senior in an undergraduate program for a bachelor's degree in Information Technology.

LinkedIn