Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to change the CursorLeft of the Console in C#?
The Console.CursorLeft property in C# allows you to set or get the horizontal position of the cursor within the console window. This property is useful for positioning text at specific columns, creating formatted output, or building text-based user interfaces.
Syntax
Following is the syntax for setting the cursor's left position −
Console.CursorLeft = columnPosition;
To get the current cursor position −
int currentPosition = Console.CursorLeft;
Parameters
The CursorLeft property accepts an integer value representing the column position (0-based indexing) where the cursor should be positioned. The value must be within the console window bounds.
Example
Let us see an example demonstrating how to change the cursor position −
using System;
class Demo {
public static void Main(string[] args) {
Console.BackgroundColor = ConsoleColor.Blue;
Console.WriteLine("Background color changed = " + Console.BackgroundColor);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("\nForeground color changed = " + Console.ForegroundColor);
Console.CursorLeft = 30;
Console.Write("CursorLeft position: " + Console.CursorLeft);
}
}
The output of the above code is −
Background color changed = Blue
Foreground color changed = Yellow
CursorLeft position: 30
Using CursorLeft for Formatted Output
Here's an example showing how to create aligned columns using CursorLeft −
using System;
class FormattedOutput {
public static void Main(string[] args) {
Console.WriteLine("Product Report:");
Console.WriteLine("================");
// Column headers
Console.CursorLeft = 0;
Console.Write("Item");
Console.CursorLeft = 15;
Console.Write("Price");
Console.CursorLeft = 25;
Console.WriteLine("Stock");
// Data rows
Console.CursorLeft = 0;
Console.Write("Laptop");
Console.CursorLeft = 15;
Console.Write("$999");
Console.CursorLeft = 25;
Console.WriteLine("25");
Console.CursorLeft = 0;
Console.Write("Mouse");
Console.CursorLeft = 15;
Console.Write("$25");
Console.CursorLeft = 25;
Console.WriteLine("150");
}
}
The output of the above code is −
Product Report: ================ Item Price Stock Laptop $999 25 Mouse $25 150
Getting Current Cursor Position
You can also retrieve the current cursor position to save and restore it later −
using System;
class CursorPosition {
public static void Main(string[] args) {
Console.Write("Starting text...");
// Save current position
int savedPosition = Console.CursorLeft;
Console.WriteLine(" Current position: " + savedPosition);
// Move cursor and write text
Console.CursorLeft = 5;
Console.WriteLine("Text at position 5");
// Restore original position
Console.CursorLeft = savedPosition;
Console.WriteLine("Back to saved position: " + Console.CursorLeft);
}
}
The output of the above code is −
Starting text... Current position: 17
Text at position 5
Back to saved position: 17
Conclusion
The Console.CursorLeft property in C# provides precise control over horizontal cursor positioning in console applications. It's essential for creating formatted output, aligned text columns, and interactive console interfaces where text positioning matters.
