[Solved] How can I make blocks in different colors behind the text in C#?


For a console application, which is what it looks like you’re working with, you are looking for Console.BackgroundColor and Console.ForegroundColor:

static void Main()
{
    Console.ForegroundColor = ConsoleColor.Red;
    Console.Write("Red");
    Console.ForegroundColor = ConsoleColor.Green;
    Console.Write("Green");
    Console.ForegroundColor = ConsoleColor.Blue;
    Console.WriteLine("Blue");

    Console.ForegroundColor = ConsoleColor.Black;
    Console.BackgroundColor = ConsoleColor.Red;
    Console.Write("Red");
    Console.BackgroundColor = ConsoleColor.Green;
    Console.Write("Green");
    Console.BackgroundColor = ConsoleColor.Blue;
    Console.WriteLine("Blue");

    Console.ResetColor();

    Console.Write("\nPress any key to exit...");
    Console.ReadKey();
}

Output

enter image description here


If you want to do a lot of this, some helper methods will come in handy:

    static void WriteBackColor(string text, ConsoleColor backColor)
    {
        Console.BackgroundColor = backColor;
        Console.Write(text);
    }

    static void WriteForeColor(string text, ConsoleColor foreColor)
    {
        Console.ForegroundColor = foreColor;
        Console.Write(text);
    }

    static void WriteColor(string text, ConsoleColor foreColor, ConsoleColor backColor)
    {
        Console.ForegroundColor = foreColor;
        Console.BackgroundColor = backColor;
        Console.Write(text);
    }

Using these cuts the original code in half:

static void Main()
{
    WriteForeColor("Red", ConsoleColor.Red);
    WriteForeColor("Green", ConsoleColor.Green);
    WriteForeColor("Blue\n", ConsoleColor.Blue);
    WriteColor("Red", ConsoleColor.Black, ConsoleColor.Red);
    WriteBackColor("Green", ConsoleColor.Green);
    WriteBackColor("Blue\n", ConsoleColor.Blue);

    Console.ResetColor();

    Console.Write("\nPress any key to exit...");
    Console.ReadKey();
}

To center text, you can calculate the padding needed on the left by getting the console width, subtracting the with of your text, and dividing by two. Then you can either set the cursor’s left position to that amount, or add that many spaces before the text:

static void Main()
{
    // Get the amount of padding needed on the left
    var leftPadding = (Console.WindowWidth - "RedGreenBlue".Length) / 2;

    // Start the cursor at that position
    Console.SetCursorPosition(leftPadding, Console.CursorTop);
    WriteForeColor("Red", ConsoleColor.Red);
    WriteForeColor("Green", ConsoleColor.Green);
    WriteForeColor("Blue\n", ConsoleColor.Blue);

    // Or, pad the left with spaces
    Console.Write(new string(' ', leftPadding));
    WriteColor("Red", ConsoleColor.Black, ConsoleColor.Red);
    WriteBackColor("Green", ConsoleColor.Green);
    WriteBackColor("Blue\n", ConsoleColor.Blue);

    Console.ResetColor();

    Console.Write("\nPress any key to exit...");
    Console.ReadKey();
}

enter image description here

3

solved How can I make blocks in different colors behind the text in C#?