-6

Hello does anybody know how am I supposed to automatically center the text once I put my program on full screen? from normal -> full screenfull screen

skrafi
  • 1
  • 2

1 Answers1

-1

The following will center one or more lines.

namespace CenterTextInWindow
{
    internal partial class Program
    {
        static void Main(string[] args)
        {
            CenterLines("Hello world");
            Console.ReadLine();
        }

        public static void CenterLines(params string[] lines)
        {

            int verticalStart = (Console.WindowHeight - lines.Length) / 2;
            int verticalPosition = verticalStart;

            foreach (var line in lines)
            {
                int horizontalStart = (Console.WindowWidth - line.Length) / 2;
                Console.SetCursorPosition(horizontalStart, verticalPosition);
                Console.Write(line);
                ++verticalPosition;
            }
        }
    }
}
Karen Payne
  • 4,341
  • 2
  • 14
  • 31