2

How can I set a minimum console size in console application C# that users can not change? Thank you in advance for answers ;)

Denis
  • 21
  • 3

1 Answers1

2

There aren't any resize Event for Console. But as a workaround you can try (not recommended!):

public static void Main(string[] args)
{
    const int MinHeight = 10;
    const int MinWidth = 10;
    Task.Factory.StartNew(() =>
    {
        while (true)
        {
            if (Console.WindowHeight > MinHeight || Console.WindowWidth > MinWidth)
            {
                Console.SetWindowSize(MinWidth, MinHeight);
            }
        }
    });
    //  Do some work here
    Console.ReadKey();
}
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
  • would this not cause 100% cpu on one core? – Cee McSharpface Mar 06 '18 at 12:15
  • of course, infinity loop. But as a workaround why not :) And the asnwer is: There aren't any resize Event for Console – Roman Marusyk Mar 06 '18 at 12:16
  • 2
    well we [could do lots of stuff](https://stackoverflow.com/a/2880337/1132334) with [this handle](https://stackoverflow.com/a/1277609/1132334). but we should not. – Cee McSharpface Mar 06 '18 at 12:21
  • Also doesn't your solution set the max height of the console, it should resize when the console height or width falls below the given min and max values not when it exceeds them. – trunks1ace Mar 06 '18 at 12:40