Itsmemonzu
Itsmemonzu

Reputation: 1

Centering Console.WriteLine text in the line

I am making a program based on the console. I want to make it look as clean as possible and I want the texts to be centered in their lines.

I tried

string msg = "Hello";

Console.SetCursorPosition((Console.WindowWidth - string.Length) / 2, Console.CursorTop);
Console.WriteLine(msg);

It worked. BUT it doesn't quite solve my problem. You see, I want multiple lines to be centered. For example

string title = "--------Message-------";
Console.SetCursorPosition((Console.WindowWidth - string.Length) / 2, Console.CursorTop);
Console.WriteLine(title);
Console.WriteLine(msg);

Now the entire thing is messed up. I hope someone can give a solution to my issue. Thanks

Upvotes: -1

Views: 179

Answers (3)

Karen Payne
Karen Payne

Reputation: 5157

Another option, use one of the following methods to start at top of screen or begin at center of screen.

public class ConsoleHelpers
{
    /// <summary>
    /// Center lines horizontally and vertically 
    /// </summary>
    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;
        }
    }
    /// <summary>
    /// Center lines vertically starting at top of screen
    /// </summary>
    public static void CenterLinesFromTop(params string[] lines)
    {

        int verticalPosition = 0;

        foreach (var line in lines)
        {
            int horizontalStart = (Console.WindowWidth - line.Length) / 2;
            Console.SetCursorPosition(horizontalStart, verticalPosition);
            Console.Write(line);
            ++verticalPosition;
        }
    }
}

Usage ConsoleHelpers.CenterLinesFromTop("Hello world","Enjoy the ride"); and ConsoleHelpers.CenterLines("Hello world","Enjoy the ride");

Upvotes: 0

Dimitar Gaydardzhiev
Dimitar Gaydardzhiev

Reputation: 71

It is not possible to create Console extension methods, but you may try something like this:

public static class ConsoleExtensions
{
    public static void WriteLineCentered(string msg)
    {
        Console.SetCursorPosition((Console.WindowWidth - msg.Length) / 2, Console.CursorTop);
        Console.WriteLine(msg);
    }
}

And then use this method, when you want to center a text:

string title = "--------Message-------";
string msg = "Hello";
ConsoleExtensions.WriteLineCentered(msg);
ConsoleExtensions.WriteLineCentered(title);

Upvotes: 2

Luaan
Luaan

Reputation: 63772

You need to centre each line individually. It's helpful to make a helper function that simply prints out a line of text in the middle of the current line rather than doing it over and over manually.

Of course, that's only going to work for text that fits within a single line - but then again, multi-line text shouldn't really be centred in the first place; that's always going to be awful to look at.

Upvotes: -1

Related Questions