Search code examples
c#consoleconsole-applicationcenterreadline

Centre a readline statement


I am about to start a project and here is what I have currently for my welcome screen.

string[] welcome = new string[4] 
{ 
    "Welcome", 
    "Choose A Option Bellow By Inputing The Number And Clicking Enter.", 
    "1. View C:\\Windows\\ File directory", 
    "2. View Your Own Custom Directory" 
};

string userChoice;
for (int x = 0; x < 4; x++)
{
    Console.WriteLine(
        "{0," + ((Console.WindowWidth / 2) + welcome[x].Length / 2) + "}", welcome[x]);
}

How do I center my read line as well? so that when the user chooses their choice will also be centered?


Solution

  • You can move the cursor using CursorLeft and CursorTop properties.
    So, in your case you would do something like this :

    Console.CursorLeft = Console.WindowWidth / 2;
    // maybe -1 to center the typed number correctly