Console Color, Text and BackgroundColor
This page was last reviewed on Nov 14, 2023.
Dot Net Perls
Console color. In a console program, colors can be used. A warning message can be printed on a red background. We set properties on Console in C#.
Shows a color consoleShows a consoleShows a color list
On a console, text and background colors can be changed—this sometimes makes programs easier to use. Errors and alerts are more noticeable.
First example. Console has many static methods and properties on it. These are static properties and methods, so they can be directly accessed.
Start The BackgroundColor property is set to ConsoleColor.Blue. ForegroundColor is set to ConsoleColor.White.
Here We see BackgroundColor and ForegroundColor. These are properties, so we do not call them like methods.
Also We use ResetColor(), which is a method. Is resets all colors on the Console.
Result The 2 lines, when they are printed, will both have blue backgrounds and white foregrounds.
Shows a color console
using System; class Program { static void Main() { // Test colors with blue background, white foreground. Console.BackgroundColor = ConsoleColor.Blue; Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("White on blue."); Console.WriteLine("Another line."); Console.ResetColor(); } }
White on blue. Another line.
Full lines. Writing an entire row of color in the Console may be helpful. It may make a good separator (like a line). Here we refactor the Console code into a separate method.
Tip In that method, you can change the colors, pad the string, and reset the console.
Detail For PadRight the parameter is the Console.WindowWidth minus one. This returns a string that will fill up the Console's width.
String PadRight, PadLeft
Also If you do not subtract one with the PadRight method, it sometimes incorrectly wraps lines.
Shows a console
using System; class Program { static void Main() { // Write one green line. WriteFullLine("This line is green."); Console.WriteLine(); // ... Write another green line. WriteFullLine("This line is also green."); Console.WriteLine(); } static void WriteFullLine(string value) { // Write an entire line to the console with the string. Console.BackgroundColor = ConsoleColor.Green; Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine(value.PadRight(Console.WindowWidth - 1)); // Reset the color. Console.ResetColor(); } }
This line is green. This line is also green.
All color enums. Here we see a program that displays all the backgrounds and foregrounds possible with the Console. It would be interesting to nest the loops and show all combinations.
Result The program will output a list of ConsoleColor values. White on blue may evoke memories of the BSOD (Blue Screen of Death).
Shows a color list
using System; class Program { static void Main() { // Demonstrate all colors and backgrounds. Type type = typeof(ConsoleColor); Console.ForegroundColor = ConsoleColor.White; foreach (var name in Enum.GetNames(type)) { Console.BackgroundColor = (ConsoleColor)Enum.Parse(type, name); Console.WriteLine(name); } Console.BackgroundColor = ConsoleColor.Black; foreach (var name in Enum.GetNames(type)) { Console.ForegroundColor = (ConsoleColor)Enum.Parse(type, name); Console.WriteLine(name); } } }
Black DarkBlue DarkGreen DarkCyan DarkRed DarkMagenta DarkYellow Gray DarkGray Blue Green Cyan Red Magenta Yellow White Black DarkBlue DarkGreen DarkCyan DarkRed DarkMagenta DarkYellow Gray DarkGray Blue Green Cyan Red Magenta Yellow White
Padding. Colors are not always the best formatting option. Try focusing on the structure of your output, with padding. We can make columns of text.
A summary. Colors, even on consoles, are beautiful. We used Console colors, including BackgroundColor and ForegroundColor. ResetColor and Console.WindowWidth are helpful.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 14, 2023 (simplify).
© 2007-2024 Sam Allen.