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 WindowTop of the Console in C#?
The Console.WindowTop property in C# gets or sets the top position of the console window relative to the screen buffer. This property is useful when you need to programmatically control the console window's vertical position within the buffer area.
Syntax
Following is the syntax to get or set the WindowTop property −
// Get the current WindowTop position int topPosition = Console.WindowTop; // Set the WindowTop position Console.WindowTop = value;
Parameters
The WindowTop property accepts an integer value representing the top row of the console window area within the screen buffer. The value must be non-negative and within the valid range of the buffer height.
Using Console.WindowTop Property
Example
Let us see an example demonstrating how to change the WindowTop of the Console −
using System;
using System.Text;
class Demo {
public static void Main(string[] args) {
Console.InputEncoding = Encoding.ASCII;
Console.WriteLine("Input Encoding Scheme = " + Console.InputEncoding);
Console.OutputEncoding = Encoding.ASCII;
Console.WriteLine("Output Encoding Scheme = " + Console.OutputEncoding);
Console.CursorVisible = false;
Console.Write("\nCursor is Visible? " + Console.CursorVisible);
Console.WindowTop = 40;
Console.Write("\nWindowTop = " + Console.WindowTop);
}
}
The output of the above code is −
Input Encoding Scheme = System.Text.ASCIIEncoding Output Encoding Scheme = System.Text.ASCIIEncoding Cursor is Visible? False WindowTop = 40
Practical Example with Window Positioning
Example
Here's a more focused example showing how to manipulate the console window position −
using System;
class WindowTopExample {
public static void Main(string[] args) {
Console.WriteLine("Current WindowTop: " + Console.WindowTop);
Console.WriteLine("Current WindowLeft: " + Console.WindowLeft);
Console.WriteLine("Buffer Height: " + Console.BufferHeight);
Console.WriteLine("Window Height: " + Console.WindowHeight);
// Move window to a different position
Console.WindowTop = 10;
Console.WriteLine("\nAfter setting WindowTop to 10:");
Console.WriteLine("New WindowTop: " + Console.WindowTop);
// Reset to original position
Console.WindowTop = 0;
Console.WriteLine("Reset WindowTop to: " + Console.WindowTop);
}
}
The output of the above code is −
Current WindowTop: 0 Current WindowLeft: 0 Buffer Height: 300 Window Height: 25 After setting WindowTop to 10: New WindowTop: 10 Reset WindowTop to: 0
Key Rules
-
The
WindowTopvalue must be non-negative. -
The value cannot exceed BufferHeight - WindowHeight.
-
Setting
WindowToponly works in console applications, not in GUI applications. -
This property may not work in all console environments or operating systems.
Conclusion
The Console.WindowTop property allows you to control the vertical position of the console window within the screen buffer. It's particularly useful for creating console applications that need precise control over window positioning and scrolling behavior.
