Export (0) Print
Expand All

Console.SetWindowSize Method

Sets the height and width of the console window to the specified values.

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

'Declaration
Public Shared Sub SetWindowSize ( _
	width As Integer, _
	height As Integer _
)
'Usage
Dim width As Integer 
Dim height As Integer 

Console.SetWindowSize(width, height)

Parameters

width
Type: System.Int32

The width of the console window measured in columns.

height
Type: System.Int32

The height of the console window measured in rows.

ExceptionCondition
ArgumentOutOfRangeException

width or height is less than or equal to zero.

-or-

width plus WindowLeft or height plus WindowTop is greater than or equal to Int16.MaxValue.

-or-

width or height is greater than the largest possible window width or height for the current screen resolution and console font.

SecurityException

The user does not have permission to perform this action.

IOException

An I/O error occurred.

This example demonstrates the SetWindowSize method, and the WindowWidth and WindowHeight properties. You must run the example to see the full effect of changing the console window size.

The example reports the dimensions of a console window set to 85 columns and 43 rows, then waits for a key to be pressed. When any key is pressed, the dimensions of the console window are halved, the new dimensions are reported, and the example waits for another key press. Finally, when any key is pressed, the console window is restored to its original dimensions and the example terminates.

' This example demonstrates the Console.SetWindowSize method, 
'                           the Console.WindowWidth property,  
'                       and the Console.WindowHeight property. 
Imports System

Class Sample
   Public Shared Sub Main()
      Dim origWidth, width As Integer 
      Dim origHeight, height As Integer 
      Dim m1 As String = "The current window width is {0}, and the " & _
                         "current window height is {1}." 
      Dim m2 As String = "The new window width is {0}, and the new " & _
                         "window height is {1}." 
      Dim m4 As String = "  (Press any key to continue...)" 
      
      ' Step 1: Get the current window dimensions. 
      '
      origWidth = Console.WindowWidth
      origHeight = Console.WindowHeight
      Console.WriteLine(m1, Console.WindowWidth, Console.WindowHeight)
      Console.WriteLine(m4)
      Console.ReadKey(True)
      
      ' Step 2: Cut the window to 1/4 its original size. 
      '
      width = origWidth / 2
      height = origHeight / 2
      Console.SetWindowSize(width, height)
      Console.WriteLine(m2, Console.WindowWidth, Console.WindowHeight)
      Console.WriteLine(m4)
      Console.ReadKey(True)
      
      ' Step 3: Restore the window to its original size. 
      '
      Console.SetWindowSize(origWidth, origHeight)
      Console.WriteLine(m1, Console.WindowWidth, Console.WindowHeight)
   End Sub 'Main
End Class 'Sample

'This example produces the following results: 

'The current window width is 85, and the current window height is 43. 
'  (Press any key to continue...) 
'The new window width is 42, and the new window height is 21. 
'  (Press any key to continue...) 
'The current window width is 85, and the current window height is 43. 

'

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

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft