Open E-School

Male
Friday, February 24, 1984

How to set console height and width using C#

Category: Programming Date: Tuesday, March 24, 2015

Goal

Learn how to resize the console window and give it a custom height and width using C#

Namespaces used

System – Console class is defined in the system namespace

Classes used

Console

Properties used

  1. Console.LargestWindowHeight – Static property in the Console class. Used to find out the largest window height which you can set for your console window depending on the current screen resolution which you have
  2. Console.LargestWindowWidth – Static property in the Console class. Used to find out the largest window width which you can set for your console window depending on the current screen resolution which you have
  3. Console.WindowHeight – Static property in the Console class. Used to change the height of the Console window using C# and give it a new height.
  4. Console.WindowWidth – Static property in the Console class. Used to change the width of the Console window using C# and give it a new width.

Brief description

You might feel a need to set the height and width of the console window to some custom size using C#. You can do this by setting the Console.WindowHeight and Console.WindowWidth properties of the console class. There is one small problem which you will find when doing so. The problem is that you are not supposed to set the height and width of the console window to any size you want. There is a maximum height and width of the console window which is allowed as per your current screen resolution. You can set the height and width of the console window only less than or equal to the largest height and width which is allowed. If you try to set the size of the console window larger than what is allowed you will get ArgumentOutOfRangeException.

So before you think of changing the height and width of your console window by using the Console.WindowHeight and Console.WindowWidth properties you should check the largest height and width allowed for your resolution by using the Console.LargestWindowHeight and Console.LargestWindowWidth properties

Code

using System;    namespace ConsoleApplication1  {      class Program      {          public static void Main(string[] args)          {              Console.WriteLine("Largest console window height allowed : " +Console.LargestWindowHeight);              Console.WriteLine("Largest console window width allowed : " + Console.LargestWindowWidth);                Console.WriteLine();              Console.WriteLine("Setting console height : 20 and width : 50");                Console.WindowHeight = 20;              Console.WindowWidth = 50;          }      }  }

Output

Console.WindowHeight and Console.WindowWidth Output

 
Explanation of the code

In the code example above we are first printing the largest height and width which is allowed for the console window by using the Console.LargestWindowHeight and Console.LargestWindowWidth properties. Then we are setting the new console height and width to 20 and 50. In the output you see that the console window height and width is set and the window is now smaller than you normally see it.

Extra Remarks

Please be careful that if you give a new height and width to your console window which is larger than the largest size allowed then you will get the ArgumentOutOfRangeException

Rate this article and help us improve

Please Login to rate
Overall ratings: 0 | Rating: out of 5
Previous Article Next Article

Quick Links

E-Magazines

@

Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..

Ok

Login to Open ESchool OR Create your account    Login   SignUp