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

.NET Framework
.NET 2.0+

Console Application Cursor Position

Many console applications are simple programs that output a series of lines of text, scrolling the console window when the available space is filled. However, by manipulating the cursor position, more complex output is made possible.

Cursor Position Properties

When you create a console application you may ask the user for input and output information, with each displayed message appearing on a new line until the window is filled. At this point, new messages cause the window to scroll, hiding previous information from view.

When you are creating more complex console applications, you may wish to enhance the user interface. For example, in a long-running process you may decide to show a progress indicator that is overwritten for each update for simple animation. More complicated scenarios may include drawing boxes that represent rudimentary, text-based windows to be updated as actions occur. This is less common with the modern Windows operating systems but useful for utilities that are needed when Windows can only be booted to a command prompt.

To obtain the position of the cursor, where the next output will be displayed, you can read the Console class's static properties, CursorLeft and CursorTop. These properties return integers that specify the number of characters between the origin of the console and the cursor position. The top-left position has both values set to zero. You can also set the properties to change the current position. If the new position that you specify is not within the visible portion of the window, the console scrolls to ensure that it is within view.

int left = Console.CursorLeft;
int top = Console.CursorTop;

SetCursorPosition Method

Another way to change the position of the cursor is with the SetCursorPosition method. This accepts two integer parameters. The first sets the CursorLeft property and the second the CursorTop value.

The following code demonstrates the use of the cursor position properties and method by drawing a series of differently coloured concentric circles. During the drawing phase the position of the cursor is changed using the properties, before outputting a space with background colour set. At the end of the program the cursor is moved back to the window's origin using SetCursorPosition.

int radius = 1;
for(ConsoleColor color = ConsoleColor.DarkBlue; color <= ConsoleColor.White; color++)
    Console.BackgroundColor = color;
    for (int i = 0; i < 360; i++)
        double radians = Math.PI * i / 180;
        Console.CursorLeft = (int)(radius * 1.5 * Math.Cos(radians)) + 40;
        Console.CursorTop = (int)(radius * Math.Sin(radians)) + 15;
        Console.Write(" ");

Console.SetCursorPosition(0, 0);
16 July 2012