Javatpoint Logo
Javatpoint Logo

Console.CursorVisible in C#

In this article, we will discuss the Console.CursorVisible property in C# with its syntax, parameters, and examples.

What is the Console.CursorVisible Property?

The CusorVisible is a property in the C# language. The name of the property says that this property is used to handle the display of the cursor, whether the cursor should be visible to the user or not. This property belongs to the Console class in the .NET framework. The value of this property is either true or false. If the Console.CursorVisible is set to true, the cursor is visible to the user. If this property is set to false, the cursor is hidden within the console window.

This property enhances the user experience by hiding and showing the cursor whenever necessary. Sometimes, the cursor will distract the user, so the user can use this property to hide it. This property is widely used in console-based games. When the user is playing the game, the programmer makes the cursor hide, and when the user goes to settings or the menu, it makes the cursor visible. When inputting in a console application, the programmer will hide the cursor using the CursorVisible property.

Syntax:

The syntax of the CursorVisible property:

To set the cursor visibility

Console.CursorVisible = true; // or false

To get the cursor visibility

bool isVisible = Console.CursorVisible;

If the isVisible variable is true, we can see the cursor in the window; otherwise, the cursor is in the hidden state, so we cannot see the cursor in the window.

Example:

Let us take a C# program to illustrate the CursorVisible property.

Output:

Console.CursorVisible in C#

Explanation:

This basic program gives information about the usage and working of the CursorVisible property. First, import the namespace "system". The main method is the entry point to the program. By default, this property is set to visible.

After that, a message is shown saying to press any key to hide the cursor. If the user clicks any key, the cursor will become invisible. Now, another message is displayed saying to click any key to make the cursor visible. Here, the CursorVisible property is set to true. Again, a message says to click any key to hide the cursor. Again, the CursorVisible property is false, making the cursor invisible.

Example 2:

Let us take another program to demonstrate the CursorVisible property in C#.

Output:

Console.CursorVisible in C#

Explanation:

This program also demonstrates the CursorVisible property. Initially, a namespace is imported in the program. A variable is initialized, named as "showCursor" and given a true value. Next, the user is shown a message to handle the cursor visibility. It says to press the letter "s" to toggle the cursor visibility and press "q" to quit.

A loop keeps the application running until the user presses the letter "q". In the while loop, an if conditional statement is used to check whether the key is available. If it is available, the program will read the key and not display the key the user pressed. After that, another if statement is used to check when the key pressed by the user is "s" or not. If the key is "s", the showCursor variable will change. If it is true initially, it is made false and vice versa. The CursorVisible property is set to the showCursor value. A message is displayed showing whether the cursor is in the visible state or a hidden state. If the key pressed by the user is "q", the application is stopped. Inside the while loop, only another message is printed saying the application is running to let the user know the application status.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA