Controlling Cursor Visibility in C# Console Applications: Using Console.CursorVisible

Learn how to control cursor visibility in C# console applications using the `Console.CursorVisible` property. This tutorial provides code examples demonstrating how to show and hide the cursor, enhancing the user experience in your console-based programs.



Controlling Cursor Visibility with `Console.CursorVisible` in C#

Introduction

The `Console.CursorVisible` property in C# allows you to control whether the cursor is displayed in the console window. This is useful for creating a better user experience, especially in console applications where the cursor might be distracting.

`Console.CursorVisible` Property

This property is a member of the `Console` class in the .NET framework. It's a boolean property, meaning it can only hold the values `true` (cursor visible) or `false` (cursor hidden).

Setting and Getting Cursor Visibility

Setting and Getting Cursor Visibility

// To show the cursor:
Console.CursorVisible = true;

// To hide the cursor:
Console.CursorVisible = false;

// To check if the cursor is visible:
bool isVisible = Console.CursorVisible;

Example 1: Basic Cursor Visibility Toggle

This example demonstrates a simple program that toggles the cursor's visibility.

Example 1: Simple Toggle

using System;

class Program {
    static void Main() {
        Console.WriteLine("Press any key to hide the cursor.");
        Console.CursorVisible = true;
        Console.ReadKey();
        Console.CursorVisible = false;
        Console.WriteLine("\nCursor hidden. Press any key to show it.");
        Console.ReadKey();
        Console.CursorVisible = true;
        Console.WriteLine("\nCursor visible again. Press any key to exit.");
        Console.ReadKey();
    }
}
Output Example 1

Press any key to hide the cursor.
(User presses a key)
Cursor hidden. Press any key to show it.
(User presses a key)
Cursor visible again. Press any key to exit.
(User presses a key)

        

Example 2: Interactive Cursor Toggle

This example allows the user to toggle the cursor using the 's' key and quit with the 'q' key.

Example 2: Interactive Toggle

using System;

class Program {
    static void Main() {
        bool showCursor = true;
        Console.WriteLine("Press 's' to toggle cursor visibility. Press 'q' to quit.");
        while (true) {
            if (Console.KeyAvailable) {
                ConsoleKeyInfo key = Console.ReadKey(true);
                if (key.Key == ConsoleKey.S) {
                    showCursor = !showCursor;
                    Console.CursorVisible = showCursor;
                    Console.WriteLine($"Cursor visibility set to: {(showCursor ? "Visible" : "Hidden")}");
                } else if (key.Key == ConsoleKey.Q) {
                    break;
                }
            }
            Console.WriteLine("Application running...");
            System.Threading.Thread.Sleep(1000);
        }
    }
}

Conclusion

The `Console.CursorVisible` property offers a simple yet effective way to manage cursor visibility in console applications. This improves the user experience, especially in games or applications where a constantly visible cursor would be distracting.