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 WindowLeft of the Console
The Console.WindowLeft property in C# gets or sets the leftmost position of the console window area relative to the screen buffer. This property controls the horizontal scrolling position of the console window within the buffer.
Syntax
Following is the syntax for using the Console.WindowLeft property −
Console.WindowLeft = value; int leftPosition = Console.WindowLeft;
Parameters
The WindowLeft property accepts an int value representing the column position (0-based) where the left edge of the console window should be positioned within the screen buffer.
Key Rules
-
The value must be greater than or equal to 0.
-
The value must be less than BufferWidth - WindowWidth to avoid going beyond buffer bounds.
-
Setting this property scrolls the console window horizontally within the buffer.
-
On some systems, this property may throw a
PlatformNotSupportedException.
Using Console.WindowLeft Property
Example
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.WindowLeft = 50;
Console.Write("WindowLeft = " + Console.WindowLeft);
}
}
The output of the above code is −
Input Encoding Scheme = System.Text.ASCIIEncoding Output Encoding Scheme = System.Text.ASCIIEncoding Cursor is Visible? False WindowLeft = 50
Demonstrating Window Position Changes
Example
using System;
class WindowLeftDemo {
public static void Main(string[] args) {
try {
Console.BufferWidth = 120;
Console.WindowWidth = 40;
Console.WriteLine("Initial WindowLeft: " + Console.WindowLeft);
Console.WriteLine("Buffer Width: " + Console.BufferWidth);
Console.WriteLine("Window Width: " + Console.WindowWidth);
Console.WindowLeft = 10;
Console.WriteLine("WindowLeft set to 10: " + Console.WindowLeft);
Console.WindowLeft = 30;
Console.WriteLine("WindowLeft set to 30: " + Console.WindowLeft);
Console.WindowLeft = 0;
Console.WriteLine("WindowLeft reset to 0: " + Console.WindowLeft);
}
catch (Exception ex) {
Console.WriteLine("Error: " + ex.Message);
}
}
}
The output of the above code is −
Initial WindowLeft: 0 Buffer Width: 120 Window Width: 40 WindowLeft set to 10: 10 WindowLeft set to 30: 30 WindowLeft reset to 0: 0
Handling Platform Compatibility
Example
using System;
class SafeWindowLeft {
public static void Main(string[] args) {
try {
int currentLeft = Console.WindowLeft;
Console.WriteLine("Current WindowLeft: " + currentLeft);
Console.WindowLeft = 20;
Console.WriteLine("Successfully set WindowLeft to: " + Console.WindowLeft);
}
catch (PlatformNotSupportedException) {
Console.WriteLine("WindowLeft property is not supported on this platform.");
}
catch (ArgumentOutOfRangeException ex) {
Console.WriteLine("Invalid WindowLeft value: " + ex.Message);
}
catch (Exception ex) {
Console.WriteLine("Unexpected error: " + ex.Message);
}
}
}
The output of the above code is −
Current WindowLeft: 0 Successfully set WindowLeft to: 20
Conclusion
The Console.WindowLeft property controls the horizontal position of the console window within the screen buffer. It's useful for creating scrollable console interfaces, but requires proper bounds checking and exception handling for cross-platform compatibility.
