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 CursorTop of the Console in C#?
The Console.CursorTop property in C# gets or sets the row position of the cursor within the console window. This property allows you to control the vertical position where text will be displayed, enabling you to create formatted console output, menus, or position text at specific locations.
Syntax
Following is the syntax for using Console.CursorTop −
// Get current cursor top position int currentTop = Console.CursorTop; // Set cursor top position Console.CursorTop = value;
Parameters
The Console.CursorTop property accepts an integer value representing the row position (0-based) where the cursor should be positioned. The value must be between 0 and Console.BufferHeight - 1.
Using Console.CursorTop to Position Text
The following example demonstrates how to change the cursor's vertical position to display text at different rows −
using System;
class Demo {
public static void Main(string[] args) {
Console.Clear();
Console.WriteLine("Initial position - CursorTop: " + Console.CursorTop);
// Move cursor to row 5
Console.CursorTop = 5;
Console.WriteLine("Text at row 5 - CursorTop: " + Console.CursorTop);
// Move cursor to row 10
Console.CursorTop = 10;
Console.WriteLine("Text at row 10 - CursorTop: " + Console.CursorTop);
// Move cursor back to row 2
Console.CursorTop = 2;
Console.WriteLine("Text inserted at row 2 - CursorTop: " + Console.CursorTop);
}
}
The output of the above code is −
Initial position - CursorTop: 0 Text inserted at row 2 - CursorTop: 2 Text at row 5 - CursorTop: 5 Text at row 10 - CursorTop: 10
Creating a Simple Menu Layout
The following example shows how to use Console.CursorTop to create a formatted menu layout −
using System;
class MenuDemo {
public static void Main(string[] args) {
Console.Clear();
// Title at top
Console.CursorTop = 2;
Console.WriteLine(" MAIN MENU");
Console.WriteLine(" =========");
// Menu options
Console.CursorTop = 6;
Console.WriteLine("1. File Operations");
Console.CursorTop = 8;
Console.WriteLine("2. Edit Options");
Console.CursorTop = 10;
Console.WriteLine("3. View Settings");
Console.CursorTop = 12;
Console.WriteLine("4. Exit Program");
// Footer message
Console.CursorTop = 16;
Console.WriteLine("Select an option (1-4):");
Console.WriteLine("Current cursor position: " + Console.CursorTop);
}
}
The output of the above code is −
MAIN MENU
=========
1. File Operations
2. Edit Options
3. View Settings
4. Exit Program
Select an option (1-4):
Current cursor position: 17
Combining CursorTop with Other Console Properties
The following example demonstrates using Console.CursorTop along with colors and other console properties −
using System;
class ColorDemo {
public static void Main(string[] args) {
Console.Clear();
// Set background and foreground colors
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.CursorTop = 3;
Console.WriteLine("Background color changed = " + Console.BackgroundColor);
Console.CursorTop = 5;
Console.WriteLine("Foreground color changed = " + Console.ForegroundColor);
// Reset colors
Console.ResetColor();
Console.CursorTop = 8;
Console.WriteLine("Colors reset - CursorTop = " + Console.CursorTop);
Console.CursorTop = 12;
Console.WriteLine("Final position reached!");
}
}
The output of the above code is −
Background color changed = Blue Foreground color changed = Yellow Colors reset - CursorTop = 8 Final position reached!
Key Rules
-
The
Console.CursorTopvalue must be between 0 andConsole.BufferHeight - 1. -
Setting an invalid position may throw an
ArgumentOutOfRangeException. -
The cursor position affects where the next
Console.Write()orConsole.WriteLine()will output text. -
Use
Console.CursorLeftto control horizontal positioning alongsideConsole.CursorTop.
Conclusion
The Console.CursorTop property provides precise control over the vertical cursor position in console applications. It's useful for creating formatted output, menus, and positioning text at specific rows within the console window.
