Console
colorSome 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
.
The Windows console supports colors. We can apply foreground (text) and background colors. We assign the ForegroundColor
and BackgroundColor
properties.
ConsoleColor
enum
to set colors. Many possible colors, like Blue, are available on this enum
.ForegroundColor
, which is the text color. We set it to white to have light-colored text.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 ModuleWhite on blue. Another line.
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 ModuleWarning Download failed Sorry
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.