Console.SetCursorPosition Method (Int32, Int32)


Sets the position of the cursor.

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

Public Shared Sub SetCursorPosition (
	left As Integer,
	top As Integer


Type: System.Int32

The column position of the cursor. Columns are numbered from left to right starting at 0.

Type: System.Int32

The row position of the cursor. Rows are numbered from top to bottom starting at 0.

Exception Condition

left or top is less than zero.


left is greater than or equal to BufferWidth.


top is greater than or equal to BufferHeight.


The user does not have permission to perform this action.


An I/O error occurred.

Use the SetCursorPosition method to specify where the next write operation in the console window is to begin. If the specified cursor position is outside the area that is currently visible in the console window, the window origin changes automatically to make the cursor visible.

The cursor automatically moves to the next character position each time a character is written to the console window. If the cursor is at the bottom right character position of the console window, the next write operation causes the console window to scroll so the cursor remains visible. If you want to write a character to the bottom right character position without causing the console window to scroll, use the MoveBufferArea method to move a character to that position.

This example demonstrates the CursorLeft and CursorTop properties, and the SetCursorPosition and Clear methods. The example positions the cursor, which determines where the next write will occur, to draw a 5 character by 5 character rectangle using a combination of "+", "|", and "-" strings. Note that the rectangle could be drawn with fewer steps using a combination of other strings.

' This example demonstrates the 
'     Console.CursorLeft and 
'     Console.CursorTop properties, and the
'     Console.SetCursorPosition and 
'     Console.Clear methods.
Imports System

Class Sample
   Protected Shared origRow As Integer
   Protected Shared origCol As Integer

   Protected Shared Sub WriteAt(s As String, x As Integer, y As Integer)
         Console.SetCursorPosition(origCol + x, origRow + y)
      Catch e As ArgumentOutOfRangeException
      End Try
   End Sub 'WriteAt

   Public Shared Sub Main()
      ' Clear the screen, then save the top and left coordinates.
      origRow = Console.CursorTop
      origCol = Console.CursorLeft

      ' Draw the left side of a 5x5 rectangle, from top to bottom.
      WriteAt("+", 0, 0)
      WriteAt("|", 0, 1)
      WriteAt("|", 0, 2)
      WriteAt("|", 0, 3)
      WriteAt("+", 0, 4)

      ' Draw the bottom side, from left to right.
      WriteAt("-", 1, 4) ' shortcut: WriteAt("---", 1, 4)
      WriteAt("-", 2, 4) ' ...
      WriteAt("-", 3, 4) ' ...
      WriteAt("+", 4, 4)

      ' Draw the right side, from bottom to top.
      WriteAt("|", 4, 3)
      WriteAt("|", 4, 2)
      WriteAt("|", 4, 1)
      WriteAt("+", 4, 0)

      ' Draw the top side, from right to left.
      WriteAt("-", 3, 0) ' shortcut: WriteAt("---", 1, 0)
      WriteAt("-", 2, 0) ' ...
      WriteAt("-", 1, 0) ' ...
      WriteAt("All done!", 0, 6)
   End Sub 'Main
End Class 'Sample
'This example produces the following results:
'|   |
'|   |
'|   |
'All done!


for modifying safe top-level windows and subwindows. Associated enumeration: UIPermissionWindow.SafeTopLevelWindows

.NET Framework
Available since 2.0
Return to top