BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Input / Output
.NET 2.0+

Modifying the Console Cursor Appearance

When creating complex software that executes as a console application it can be useful to change the appearance of the cursor. The Console class permits some level of customisation, allowing the cursor size to be adjusted or the cursor to be hidden.

Cursor Size

One of the modifications you can make to the blinking text cursor of a console application is to change its size. You might want to do this to indicate the state of your software. For example, programs that permit text entry might show a thin horizontal line at the base of the cursor when accepting input normally, or a full blinking block when in an over-typing mode. You are quite limited in the shape of the cursor. You can only set the size of the flashing block as a percentage of the overall character height.

You set the size using the static property, CursorSize, which is defined in the Console class. The value must be an integer between one and one hundred. The number specifies the percentage of the cursor position's character that will be filled with colour. A value of one creates a thin line at the base of the character at the cursor's location. Higher values extend the cursor block upwards, with the maximum 100% causing the entire character space to be filled.

You can see an example of the use of this property in the code sample at the end of the article. You can also download the program using the link at the top.

Showing and Hiding the Cursor

In some programs the cursor is not required and may be a distraction for the user. For example, if the console application is used only to display pages of information in a similar manner to the Teletext system, which originated in the UK and was later used world-wide. In such situations it makes sense to remove the blinking cursor temporarily or permanently.

You can hide the cursor by setting the Console.CursorVisible property to false. If you later wish to redisplay the cursor, set this Boolean property back to true. The cursor will reappear with its size determined by the CursorSize property.

Example Program

The code below uses the two above properties, along with other Console properties and methods that set colours, clear the console, change the program's title and set the cursor position. The program allows you to experiment with the two cursor display properties by pressing H, S or X.

static void Main(string[] args)
{
    ConsoleKey key;
    Console.Title = "Cursor Display Sample";

    do
    {
        ShowCursorState();
        key = Console.ReadKey(true).Key;
        ProcessKey(key);
    } while (key != ConsoleKey.Escape);
}

private static void ShowCursorState()
{
    Console.Clear();
    Console.ResetColor();
    Console.WriteLine("H to Hide, S to Show, X to Increase Height, Escape to Quit");
    Console.WriteLine();
    Console.ForegroundColor = ConsoleColor.Yellow;
    Console.WriteLine(Console.CursorVisible ? "Visible" : "Hidden");
    Console.WriteLine("Size: {0}%", Console.CursorSize);
    Console.WriteLine("\nX");
    Console.SetCursorPosition(0, 5);
}

private static void ProcessKey(ConsoleKey key)
{
    switch (key)
    {
        case ConsoleKey.H:
        Console.CursorVisible = false;
        break;

        case ConsoleKey.S:
        Console.CursorVisible = true;
        break;

        case ConsoleKey.X:
        Console.CursorSize = Console.CursorSize == 100 ? 1 : Console.CursorSize + 1;
        break;
    }
}

Try running the program and using the keys to change the size and visibility of the cursor. The results should be similar to the image below. Note the cursor positioned over the X at the bottom of the text. In this example the cursor is visible and its size is set to fifty.

Console Cursor Display Properties

13 October 2013