Console.Cursorvisible In C#

In this post, we are going to explore the Console.CursorVisible property in C# along with its syntax, arguments, and demonstrations.

What is the Console.CursorVisible Property?

The CusorVisible property is a feature in C# programming. Its purpose is to control the visibility of the cursor on the screen, determining whether it should be displayed to the user or not. This property is a member of the Console class within the .NET framework. Its value can be either true or false. When Console.CursorVisible is true, the cursor is shown to the user, while setting it to false will hide the cursor within the console window.

This feature improves user interaction by toggling the visibility of the cursor as needed. At times, the cursor could be disruptive to the user, prompting the user to utilize this feature to conceal it. This functionality is commonly employed in text-based games. During gameplay, developers can hide the cursor, and when users navigate to menus or settings, the cursor becomes visible. In console applications, programmers can manage cursor visibility by adjusting 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 set to true, the cursor will be visible within the window; if it is false, the cursor will be hidden and not visible within the window.

Example:

Let's consider a C# code example to demonstrate the functionality of the CursorVisible property.

Example

using System;
class Program
{
    static void Main()
    {
        Console.WriteLine("Press any key to hide the cursor.");
        // Display the cursor
        Console.CursorVisible = true;
        Console.ReadKey();
        // Hide the cursor
        Console.CursorVisible = false;
        Console.WriteLine("\nCursor hidden. Press any key to show it.");
        Console.ReadKey();
        // Show the cursor again
        Console.CursorVisible = true;
        Console.WriteLine("\nCursor visible again. Press any key to exit.");
        // Wait for user input before exiting
        Console.ReadKey();
    }
}

Output:

The <style> component includes a CSS class named placeholder-diagram. This class defines styling properties such as background color, border radius, padding, margin, and text alignment for a specific element. Within this class, there are also styles for a placeholder icon and text, controlling their font size, margin, and color. These styles collectively contribute to the visual presentation of the placeholder element on a webpage.

Explanation:

This simple code snippet provides insights into the functionality and usage of the CursorVisible attribute. Initially, import the "system" namespace. The main function serves as the starting point of the application. By default, the visibility state of this attribute is set to visible.

Following this, a prompt will appear instructing the user to press a key in order to conceal the cursor. Upon pressing any key, the cursor will be hidden from view. Subsequently, a new message will be presented, indicating that pressing any key will render the cursor visible again. At this point, the CursorVisible property is adjusted to true. Another prompt will then appear, requesting the user to press any key to hide the cursor once more. Upon pressing any key, the CursorVisible property is set to false, resulting in the cursor becoming invisible.

Example 2:

Let's consider another example to showcase the CursorVisible property in C#.

Example

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);
        }
    }
}

Output:

The CSS code snippet below demonstrates the styling of a placeholder diagram using a linear gradient background, border radius, padding, margin, and text alignment properties:

Example

.placeholder-diagram { background: linear-gradient(135deg, #374151 0%, #1f2937 100%); border-radius: 12px; padding: 40px; margin: 20px 0; text-align: center; }
.placeholder-diagram .placeholder-icon { font-size: 3rem; margin-bottom: 10px; }
.placeholder-diagram .placeholder-text { color: #9ca3af; font-size: 1rem; }

Explanation:

This code snippet showcases the CursorVisible attribute as well. At the beginning of the program, an external namespace is imported. A variable called "showCursor" is declared and assigned a boolean value of true. Subsequently, a prompt instructs the user on managing the cursor visibility. It directs the user to press the key "s" to switch the cursor visibility and "q" to exit the program.

A continuous loop is responsible for maintaining the application's functionality until the user triggers the termination by pressing the letter "q". Within this loop, an if statement is employed to verify the presence of a pressed key. Upon detection, the program will capture the key press without revealing it. Subsequently, another if condition is utilized to determine if the user has pressed the key "s". If the key corresponds to "s", the showCursor variable undergoes a toggle, switching between true and false values. The CursorVisible property is then updated based on the current showCursor status. A notification is presented to indicate whether the cursor is visible or hidden. In the event that the user presses "q", the execution of the application halts. Additionally, a notification is displayed within the loop to inform the user about the continuous operation of the application.

Input Required

This code uses input(). Please provide values below: