Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Console.WindowLeft Property

Gets or sets the leftmost position of the console window area relative to the screen buffer.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public static int WindowLeft { get; set; }
/** @property */
public static int get_WindowLeft ()

/** @property */
public static void set_WindowLeft (int value)

public static function get WindowLeft () : int

public static function set WindowLeft (value : int)

Not applicable.

Property Value

The leftmost console window position measured in columns.

Exception typeCondition

IOException

Error reading or writing information.

The following example demonstrates the WindowLeft, WindowTop, WindowWidth, WindowHeight, BufferWidth, BufferHeight, and CursorVisible properties; and the SetWindowPosition, SetBufferSize, and ReadKey methods. The example draws a grid pattern in the screen buffer based on the screen buffer width. Then the example moves the console window in response to which of the UP ARROW, DOWN ARROW, LEFT ARROW, or RIGHT ARROW console keys is pressed. The grid pattern helps you see the movement of the console window relative to the screen buffer.

// This example demonstrates the Console.WindowLeft and
//                               Console.WindowTop properties.
using System;
using System.Text;
using System.IO;
//
class Sample 
{
    public static int saveBufferWidth;
    public static int saveBufferHeight;
    public static int saveWindowHeight;
    public static int saveWindowWidth;
    public static bool saveCursorVisible;
//
    public static void Main() 
    {
    string m1 = "1) Press the cursor keys to move the console window.\n" +
                "2) Press any key to begin. When you're finished...\n" +
                "3) Press the Escape key to quit.";
    string g1 = "+----";
    string g2 = "|    ";
    string grid1;
    string grid2;
    StringBuilder sbG1 = new StringBuilder();
    StringBuilder sbG2 = new StringBuilder();
    ConsoleKeyInfo cki;
    int y;
//
    try 
    {
    saveBufferWidth  = Console.BufferWidth;
    saveBufferHeight = Console.BufferHeight;
    saveWindowHeight = Console.WindowHeight;
    saveWindowWidth  = Console.WindowWidth;
    saveCursorVisible = Console.CursorVisible;
//
    Console.Clear();
    Console.WriteLine(m1);
    Console.ReadKey(true);

// Set the smallest possible window size before setting the buffer size.
    Console.SetWindowSize(1, 1);
    Console.SetBufferSize(80, 80);
    Console.SetWindowSize(40, 20);

// Create grid lines to fit the buffer. (The buffer width is 80, but
// this same technique could be used with an arbitrary buffer width.)
    for (y = 0; y < Console.BufferWidth/g1.Length; y++)
        {
        sbG1.Append(g1);
        sbG2.Append(g2);
        }
    sbG1.Append(g1, 0, Console.BufferWidth%g1.Length);
    sbG2.Append(g2, 0, Console.BufferWidth%g2.Length);
    grid1 = sbG1.ToString();
    grid2 = sbG2.ToString();

    Console.CursorVisible = false;
    Console.Clear();
    for (y = 0; y < Console.BufferHeight-1; y++)
        {
        if (y%3 == 0)
            Console.Write(grid1);
        else
            Console.Write(grid2);
        }

    Console.SetWindowPosition(0, 0);
    do
        {
        cki = Console.ReadKey(true);
        switch (cki.Key) 
            {
            case ConsoleKey.LeftArrow:
                if (Console.WindowLeft > 0) 
                    Console.SetWindowPosition(
                            Console.WindowLeft-1, Console.WindowTop);
                break;
            case ConsoleKey.UpArrow:
                if (Console.WindowTop > 0) 
                    Console.SetWindowPosition(
                            Console.WindowLeft, Console.WindowTop-1);
                break;
            case ConsoleKey.RightArrow:
                if (Console.WindowLeft < (Console.BufferWidth-Console.WindowWidth)) 
                    Console.SetWindowPosition(
                            Console.WindowLeft+1, Console.WindowTop);
                break;
            case ConsoleKey.DownArrow:
                if (Console.WindowTop < (Console.BufferHeight-Console.WindowHeight)) 
                    Console.SetWindowPosition(
                            Console.WindowLeft, Console.WindowTop+1);
                break;
            }
        } 
    while (cki.Key != ConsoleKey.Escape);  // end do-while
    } // end try
    catch (IOException e) 
        {
        Console.WriteLine(e.Message);
        }
    finally 
        {
        Console.Clear();
        Console.SetWindowSize(1, 1);
        Console.SetBufferSize(saveBufferWidth, saveBufferHeight);
        Console.SetWindowSize(saveWindowWidth, saveWindowHeight);
        Console.CursorVisible = saveCursorVisible;
        }
    } // end Main
} // end Sample
/*
This example produces results similar to the following:

1) Press the cursor keys to move the console window.
2) Press any key to begin. When you're finished...
3) Press the Escape key to quit.

...

+----+----+----+-
|    |    |    |
|    |    |    |
+----+----+----+-
|    |    |    |
|    |    |    |
+----+----+----+-

*/

// This example demonstrates the Console.WindowLeft and
//                               Console.WindowTop properties.
import System.*;
import System.Text.*;
import System.IO.*;
//
class Sample
{
    public static int saveBufferWidth;
    public static int saveBufferHeight;
    public static int saveWindowHeight;
    public static int saveWindowWidth;
    public static boolean saveCursorVisible;
    //
    public static void main(String[] args)
    {
        String m1 = "1) Press the cursor keys to move the console window.\n"
            + "2) Press any key to begin. When you're finished...\n"
            + "3) Press the Escape key to quit.";
        String g1 = "+----";
        String g2 = "|    ";
        String grid1;
        String grid2;
        StringBuilder sbG1 = new StringBuilder();
        StringBuilder sbG2 = new StringBuilder();
        ConsoleKeyInfo cki;
        int y;
        //
        try {
            saveBufferWidth = Console.get_BufferWidth();
            saveBufferHeight = Console.get_BufferHeight();
            saveWindowHeight = Console.get_WindowHeight();
            saveWindowWidth = Console.get_WindowWidth();
            saveCursorVisible = Console.get_CursorVisible();
            //
            Console.Clear();
            Console.WriteLine(m1);
            Console.ReadKey(true);

            // Set the smallest possible window size before setting the buffer
            // size.
            Console.SetWindowSize(1, 1);
            Console.SetBufferSize(80, 80);
            Console.SetWindowSize(40, 20);

            // Create grid lines to fit the buffer. (The buffer width is 80, but
            // this same technique could be used with an arbitrary buffer width.)
            for (y = 0; y < Console.get_BufferWidth() / g1.get_Length(); y++) {
                sbG1.Append(g1);
                sbG2.Append(g2);
            }

            sbG1.Append(g1, 0, Console.get_BufferWidth() % g1.get_Length());
            sbG2.Append(g2, 0, Console.get_BufferWidth() % g2.get_Length());
            grid1 = sbG1.ToString();
            grid2 = sbG2.ToString();

            Console.set_CursorVisible(false);
            Console.Clear();
            for (y = 0; y < Console.get_BufferHeight() - 1; y++) {
                if (y % 3 == 0) {
                    Console.Write(grid1);
                }
                else {
                    Console.Write(grid2);
                }
            }
            Console.SetWindowPosition(0, 0);
            do {
                cki = Console.ReadKey(true);
                switch (cki.get_Key()) {
                    case ConsoleKey.LeftArrow :
                        if (Console.get_WindowLeft() > 0) {
                            Console.SetWindowPosition(Console.get_WindowLeft()-1,
                                Console.get_WindowTop());
                        }
                        break;
                    case ConsoleKey.UpArrow :
                        if (Console.get_WindowTop() > 0) {
                            Console.SetWindowPosition(Console.get_WindowLeft(),
                                Console.get_WindowTop() - 1);
                        }
                        break;
                    case ConsoleKey.RightArrow :
                        if (Console.get_WindowLeft() < Console.get_BufferWidth()
                            - Console.get_WindowWidth()) {
                            Console.SetWindowPosition(Console.get_WindowLeft()+1,
                                Console.get_WindowTop());
                        }
                        break;
                    case ConsoleKey.DownArrow :
                        if (Console.get_WindowTop() < Console.get_BufferHeight()
                            - Console.get_WindowHeight()) {
                            Console.SetWindowPosition(Console.get_WindowLeft(),
                                Console.get_WindowTop() + 1);
                        }
                        break;
                }
            } while (!(cki.get_Key().Equals(ConsoleKey.Escape))); // end do-while
        } // end try
        catch (IOException e) {
            Console.WriteLine(e.get_Message());
        }
        finally {
            Console.Clear();
            Console.SetWindowSize(1, 1);
            Console.SetBufferSize(saveBufferWidth, saveBufferHeight);
            Console.SetWindowSize(saveWindowWidth, saveWindowHeight);
            Console.set_CursorVisible(saveCursorVisible);
        }
    } //end main
} //end Sample
/*
This example produces results similar to the following:

1) Press the cursor keys to move the console window.
2) Press any key to begin. When you're finished...
3) Press the Escape key to quit.

...

+----+----+----+-
|    |    |    |
|    |    |    |
+----+----+----+-
|    |    |    |
|    |    |    |
+----+----+----+-

*/

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft