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
C# Console.WindowLeft Property
The Console.WindowLeft property in C# gets or sets the leftmost position of the console window area relative to the screen buffer. This property is useful when you need to control or retrieve the horizontal position of the console window within the buffer area.
Syntax
Following is the syntax to get the left position of the console window −
int position = Console.WindowLeft;
Following is the syntax to set the left position of the console window −
Console.WindowLeft = value;
Return Value
The property returns an int value representing the column position of the left edge of the console window relative to the screen buffer.
Getting Console Window Left Position
Example
using System;
class Demo {
static void Main() {
int left;
left = Console.WindowLeft;
Console.WriteLine("Left position of the Console window = " + left);
Console.WriteLine("Buffer width: " + Console.BufferWidth);
Console.WriteLine("Window width: " + Console.WindowWidth);
}
}
The output of the above code is −
Left position of the Console window = 0 Buffer width: 120 Window width: 120
Setting Console Window Left Position
Example
using System;
class WindowPositionDemo {
static void Main() {
Console.WriteLine("Initial left position: " + Console.WindowLeft);
// Set buffer size larger than window to allow positioning
Console.SetBufferSize(150, 50);
Console.SetWindowSize(80, 25);
// Set the left position
Console.WindowLeft = 10;
Console.WriteLine("New left position: " + Console.WindowLeft);
Console.WriteLine("Window can be positioned within the buffer area.");
}
}
The output of the above code is −
Initial left position: 0 New left position: 10 Window can be positioned within the buffer area.
Working with Window Boundaries
Example
using System;
class WindowBoundaryDemo {
static void Main() {
try {
Console.SetBufferSize(100, 30);
Console.SetWindowSize(60, 20);
Console.WriteLine("Buffer Width: " + Console.BufferWidth);
Console.WriteLine("Window Width: " + Console.WindowWidth);
Console.WriteLine("Maximum possible left position: " + (Console.BufferWidth - Console.WindowWidth));
// Set window to rightmost position
Console.WindowLeft = Console.BufferWidth - Console.WindowWidth;
Console.WriteLine("Window moved to rightmost position: " + Console.WindowLeft);
}
catch (Exception ex) {
Console.WriteLine("Error: " + ex.Message);
}
}
}
The output of the above code is −
Buffer Width: 100 Window Width: 60 Maximum possible left position: 40 Window moved to rightmost position: 40
Key Rules
-
The
WindowLeftvalue cannot exceedBufferWidth - WindowWidth. -
The property value cannot be negative.
-
Setting an invalid position throws an
ArgumentOutOfRangeException. -
The buffer size must be larger than the window size to allow positioning.
Conclusion
The Console.WindowLeft property provides control over the horizontal positioning of the console window within the screen buffer. It is essential for creating applications that need precise control over console window placement and navigation within larger buffer areas.
