how can I keep all of my outputs centered in Visual Studios as well as my cursor?

3578 views c#

I am trying to create a sort of an interactive menu (of all my past codes). How can I keep my output text centered as well as ensure the cursor is centered when a user enters in information?

For centering my outputs, I've just been using a lot of spaces. As far as centering the cursor, I have no idea where to start.

currently just using a ton of spaces

  Console.WriteLine("\r\n\r\n                                  Welcome, Please Enter An Encrypted Message Using:");
        Console.WriteLine("\r\n                                                     @ For A");
        Console.WriteLine("\r\n                                                     # For E");
        Console.WriteLine("\r\n                                                     ^ For I");
        Console.WriteLine("\r\n                                                     * For O");
        Console.WriteLine("\r\n                                                     + For U");

answered question

1 Answer


You can try to use Console.SetCursorPosition function with some calculation let the word in the centre.

static void SetTextCentre(string s) {
    Console.SetCursorPosition((Console.WindowWidth - s.Length) / 2, Console.CursorTop);

then using like

SetTextCentre("Welcome, Please Enter An Encrypted Message Using:");
SetTextCentre("@ For A");
SetTextCentre("# For E");
SetTextCentre("^ For I");
SetTextCentre("* For O");
SetTextCentre("+ For U");

posted this

Have an answer?


Please login first before posting an answer.