Home > OS >  Trying to make a loop for console window size
Trying to make a loop for console window size

Time:11-05

I'm trying to make a loop that will print a message if the window size is too small while waiting for the user to press enter. Once the user presses enter, I want it to check again if the console window is big enough before it exits the loop

{
    WriteLine("The current terminal size is to small to show the race track.");
    WriteLine("Please resize the window to atleast 64 character wide and 12 lines high.");
    WriteLine("Please press [enter] to continue");
    ReadLine();
}

CodePudding user response:

Check for a specific window size: if (Console.WindowWidth < 120 || Console.WindowHeight < 30) {}

In case you want the program to change it for you: Console.SetWindowSize(120, 30);

CodePudding user response:

Here's a more fully fleshsed out version...

namespace ConsoleApp1
{
    internal class Program
    {
        static int minWidth = 120;
        static int minHeight = 12;
        static void Main(string[] args)
        {
            while (WindowIsTooSmall())
            {
                Console.WriteLine("Make it bigger, then press enter");
                Console.ReadLine();
            }
        }
        static bool WindowIsTooSmall()
        {
            return (Console.WindowWidth < minWidth || Console.WindowWidth < minHeight);
        }
    }
}

Make your message and variables what you want them to be to suit your needs. Put the variables at the right application level (global, whatever).

Hopefully this helps

  •  Tags:  
  • c#
  • Related