Changing Console Window's size throws ArgumentOutOfRangeException
Asked Answered
R

5

24

I am trying to set the size of the Console Window in a c# console application. I get an ArgumentOutOfRangeException with this message:

The value must be less than the console's current maximum window size of 41 in that dimension. Note that this value depends on screen resolution and the console font.

I am using this to set it:

Console.WindowHeight = 480;

How do you set the Console window's size properly?

Responsibility answered 26/2, 2013 at 21:23 Comment(2)
Console height is specified in rows (lines), not pixels.Greenlet
That was the problem. If you post that as an answer I'll accept it.Responsibility
H
50

From MSDN of Console.WindowHeight property:

The height of the console window measured in rows.

As you can see, these are not pixels. Just remember, these values can change depending on your screen resolution and the console font. You can find maximum height and width values with Console.LargestWindowWidth and Console.LargestWindowHeight properties.

Console.WriteLine(Console.LargestWindowHeight);
Console.WriteLine(Console.LargestWindowWidth);
Headachy answered 26/2, 2013 at 21:33 Comment(4)
Just noticed this more detailed answer. Changed it to the accepted oneResponsibility
On my PC those properties are way out of range (x=1904 ??? / ymax=62) Any idea ?Exodontist
@MarioFavere Because these values are based on the current font and screen resolution.Benevento
I do not have 1904 columns on the screen ;-)Exodontist
G
1

Console height is specified in rows (lines), not pixels.

http://msdn.microsoft.com/en-us/library/system.console.windowheight.aspx

Greenlet answered 27/2, 2013 at 22:52 Comment(0)
S
0

Microsoft recently published some information around this, see:

  1. Understanding Windows Console Host Settings

Try this in powershell:

$windowSize = $(get-item hkcu:\console).GetValue("WindowSize")
$windowHeight = $windowSize -shr 16
$windowWidth = ($windowSize -shl 16) -shr 16
Selfevident answered 12/7, 2019 at 20:13 Comment(0)
D
0

When I set the Window width to say 130, and when I click the maximise button in Console App, the Console window width is not chnanging in the console application. Can anyone please check and confirm ?

if (Console.WindowWidth == Console.LargestWindowWidth || Console.WindowWidth == Console.LargestWindowHeight) 
{ 
    Console.WindowWidth = 130;
    Console.WindowHeight = 30;
}
Damnable answered 21/2 at 18:17 Comment(2)
Welcome. Please post this as your own question, with a link to this question here, to notify that it's related. Good tips in How to Ask, How to Answer and tourRespire
If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From ReviewRespire
S
-4

you can set a windowHeight less than 62, if u try exceed this value error throw the system.

class Pro
{
    public static void fun()
    {
        Console.WindowHeight = 61;
        Console.WriteLine("Welcome to asp .net ");
    }


    static void Main(string[] args)
    {
        Pro.fun();
    }

    // Summary:
    //     Gets the largest possible number of console window rows, based on the current
    //     font and screen resolution.
    //
    // Returns:
    //     The height of the largest possible console window measured in rows.
    public static int LargestWindowHeight { get; }

    // Summary:
    //     Gets the largest possible number of console window columns, based on the
    //     current font and screen resolution.
    //
    // Returns:
    //     The width of the largest possible console window measured in columns.
    public static int LargestWindowWidth { get; }

The above information catch Console[from metadata].

Skurnik answered 26/2, 2013 at 21:41 Comment(2)
U can with u'r font and display settings. The rest of us are better off checking the LargestWindowHeight property.Falla
As Habo says; this is only the max value for you. The actual maximum value will depend on every person's screen size. Not just the programmer's screen size/res - but the end-user's. Also, you don't even explain at all why it has to be less than 62; What does "62" mean in this context? (I know the answer - but your answer does not say)Bloodstone

© 2022 - 2024 — McMap. All rights reserved.