Home
VB.NET
Console Color Use
Updated Nov 14, 2023
Dot Net Perls
Console color. Some programs are best implemented with complex graphical controls, but often using console output is sufficient. And in VB.NET we can improve console display with colors.
By assigning enums to some properties, we can adjust the background and foreground (text) color. Colors remain set until we reset them with ResetColor.
Example. The Windows console supports colors. We can apply foreground (text) and background colors. We assign the ForegroundColor and BackgroundColor properties.
Step 1 We must use the ConsoleColor enum to set colors. Many possible colors, like Blue, are available on this enum.
Enum
Step 2 We set the ForegroundColor, which is the text color. We set it to white to have light-colored text.
Step 3 The ResetColor subroutine changes the color scheme (both foreground and background) to the default.
Module Module1 Sub Main() ' Step 1: set background to blue. Console.BackgroundColor = ConsoleColor.Blue ' Step 2: set foreground to white. Console.ForegroundColor = ConsoleColor.White ' Step 3: write some text, and reset the colors. Console.WriteLine("White on blue.") Console.WriteLine("Another line.") Console.ResetColor() End Sub End Module
White on blue. Another line.
Different color example. Many colors are available in .NET. We can use bright red with Console.Red to signify a concerning error or warning.
Module Module1 Sub Main() ' Set Foreground and Background colors. ' ... You can just set one. Console.ForegroundColor = ConsoleColor.Red Console.BackgroundColor = ConsoleColor.DarkCyan Console.WriteLine("Warning") Console.WriteLine("Download failed") ' Reset the colors. Console.ResetColor() Console.WriteLine("Sorry") End Sub End Module
Warning Download failed Sorry
Summary. Though it will not be the same as a native Windows Forms program, a console program with colors is often more enjoyable to use than one without colors.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Nov 14, 2023 (new).
Home
Changes
© 2007-2025 Sam Allen