Console.WindowTop Property


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

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

Public Shared Property WindowTop As Integer

Property Value

Type: System.Int32

The uppermost console window position measured in rows.

Exception Condition

In a set operation, the value to be assigned is less than zero.


As a result of the assignment, WindowTop plus WindowHeight would exceed BufferHeight.


Error reading or writing information.

The console represents a rectangular window into a larger rectangular buffer area. Both the window and the buffer are measured vertically by their number of rows and horizontally by their number of columns. The dimensions of the buffer area are defined by the BufferHeight and BufferWidth properties. The dimensions of the console area are defined by the WindowHeight and WindowWidth properties. The WindowTop property determines which row of the buffer area is displayed in the first column of the console window. The value of the WindowTop property can range from 0 to BufferHeight - WindowHeight. Attempting to set it to a value outside that range throws an ArgumentOutOfRangeException.

Attempting to set the value of the WindowTop property when output is redirected throws an IOException exception. To prevent the exception, you can set the value of this property only if the IsOutputRedirected property returns false.

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.
Imports System.IO
Imports System.Text

Class Sample
   Public Shared saveBufferWidth As Integer
   Public Shared saveBufferHeight As Integer
   Public Shared saveWindowHeight As Integer
   Public Shared saveWindowWidth As Integer
   Public Shared saveCursorVisible As Boolean

   Public Shared Sub Main()
      Dim m1 As String = _
          "1) Press the cursor keys to move the console window." & vbCrlf & _
          "2) Press any key to begin. When you're finished..."   & vbCrlf & _
          "3) Press the Escape key to quit."
      Dim g1 As String = "+----"
      Dim g2 As String = "|    "
      Dim grid1 As String
      Dim grid2 As String
      Dim sbG1 As New StringBuilder()
      Dim sbG2 As New StringBuilder()
      Dim cki As ConsoleKeyInfo
      Dim y As Integer
         saveBufferWidth = Console.BufferWidth
         saveBufferHeight = Console.BufferHeight
         saveWindowHeight = Console.WindowHeight
         saveWindowWidth = Console.WindowWidth
         saveCursorVisible = Console.CursorVisible

         ' 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 To (Console.BufferWidth / g1.Length) - 1
         Next y
         sbG1.Append(g1, 0, Console.BufferWidth Mod g1.Length)
         sbG2.Append(g2, 0, Console.BufferWidth Mod g2.Length)
         grid1 = sbG1.ToString()
         grid2 = sbG2.ToString()

         Console.CursorVisible = False
         For y = 0 To (Console.BufferHeight - 2)
            If y Mod 3 = 0 Then
            End If
         Next y 
         Console.SetWindowPosition(0, 0)
            cki = Console.ReadKey(True)
            Select Case cki.Key
               Case ConsoleKey.LeftArrow
                  If Console.WindowLeft > 0 Then
                     Console.SetWindowPosition(Console.WindowLeft - 1, Console.WindowTop)
                  End If
               Case ConsoleKey.UpArrow
                  If Console.WindowTop > 0 Then
                     Console.SetWindowPosition(Console.WindowLeft, Console.WindowTop - 1)
                  End If
               Case ConsoleKey.RightArrow
                  If Console.WindowLeft < Console.BufferWidth - Console.WindowWidth Then
                     Console.SetWindowPosition(Console.WindowLeft + 1, Console.WindowTop)
                  End If
               Case ConsoleKey.DownArrow
                  If Console.WindowTop < Console.BufferHeight - Console.WindowHeight Then
                     Console.SetWindowPosition(Console.WindowLeft, Console.WindowTop + 1)
                  End If
            End Select
         Loop While cki.Key <> ConsoleKey.Escape
      ' end do-while
      ' end try
      Catch e As IOException
         Console.SetWindowSize(1, 1)
         Console.SetBufferSize(saveBufferWidth, saveBufferHeight)
         Console.SetWindowSize(saveWindowWidth, saveWindowHeight)
         Console.CursorVisible = saveCursorVisible
      End Try
   End Sub 'Main ' end Main
End Class 'Sample ' 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.
'|    |    |    |
'|    |    |    |
'|    |    |    |
'|    |    |    |

.NET Framework
Available since 2.0
Return to top