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.
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 ModuleWhite 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 ModuleWarning
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 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.