Colorful Console Mode Applications in Visual Basic

Introduction:

So, you need to a write a console mode application, no reason you can't have a little fun with it by introducing color into the output.  This article describes how to jazz up a console mode application with color (which, as you will see, is pretty darned easy to do).

Application-in-VB.NET.gif

Figure 1:  The Application

Getting Started:

In order to get started, unzip the included project and open the solution in the Visual Studio 2008 environment.   In the solution explorer, you should note these files (Figure 2):

Solution-Explorer-in-VB.NET.gif

Figure 2:  Solution Explorer

As you can see from Figure 2; there is a single console mode application entitled, "ColorfulConsole_VB" which contains only the Program.vb file.  All of the code used in the example project is contained in the Program.vb file.

The Application (Program.vb)

If you'd care to open the code view up into the IDE you will see that the code file begins with the following library imports (which are the defaults):

Imports System

Imports System.Collections.Generic

Imports System.Linq

Imports System.Text

Following the imports, the module is declared:

Module Module1

 

Next up, the main function begins by created a typed list of accounts; the account class is nested in the Program.vb file and is used to capture some fake account information so that there is something to display.

 

    Sub Main()

 

        ' create some dummy data so we have

        ' something to display

        Dim accts As List(Of Accounts) = CreateAccounts()

 

After the list is created, the console's foreground and background color properties are set.  That is all that is needed to color the console application's output.

 

        ' set the foreground and background colors

        ' using the console's Foreground and BackgroundColor

        ' properties - here we are setting it up to show

        ' white characters on a dark blue background

        Console.ForegroundColor = ConsoleColor.White

        Console.BackgroundColor = ConsoleColor.DarkBlue

 

After the colors are set, a banner is sent to the display; the banner will appear as white text on a blue field:

 

        ' write the banner to screen using writeline

        Console.WriteLine("*****************************************************")

        Console.WriteLine("*                                                   *")

        Console.WriteLine("*        Summary of Accounts (By Last Name)         *")

        Console.WriteLine("*                                                   *")

        Console.WriteLine("*****************************************************")

 

 

        ' add a couple of new lines to break up the banner

        ' from the rest of the text

        Console.Write(Environment.NewLine + Environment.NewLine)

 

After the banner has been sent to output, an LINQ to Objects query is used to sort the list by the account holder's last name:

 

        ' use Linq to Objects to order the list by last name

        Dim q = _

            (From ac In accts _

            Order By ac.LastName Ascending _

            Select ac).ToList()

 

After sorting the list alphabetically but the account holder's last name, the code goes through the list item by item and writes the account information to the screen.  The code is annotated to describe each section but in general, the intent was to write the labels in white and the values is some other color; the account balances were treated differently to show positive balances in green and negative balances in red.  Note that to use multiple colors on a single line, you need only set the color, use the Console Write (in lieu of WriteLine) to write out a partial line; reset the color and the write out the rest of the line in the other color.

 

        ' display the list in the console

        Dim a As New Accounts()

 

        For Each a In q

 

            ' set the foreground and background colors

            ' using the console's Foreground and BackgroundColor

            ' properties - here we are setting it up to show

            ' white characters on a black background

            Console.ForegroundColor = ConsoleColor.White

            Console.BackgroundColor = ConsoleColor.Black

 

            ' write out the Name title using Console.Write

            Console.Write("Name: ")

 

            ' change the foreground color and finish the

            ' line with the name of the account holder

            ' (two colors in one line)

            Console.ForegroundColor = ConsoleColor.Cyan

            Console.BackgroundColor = ConsoleColor.Black

            Console.Write(Space(10) + a.LastName + ", " + a.FirstName + " " +

            a.MiddleName + Environment.NewLine)

 

            ' reset to white characters on black

            ' and write out the next line title

            Console.ForegroundColor = ConsoleColor.White

            Console.BackgroundColor = ConsoleColor.Black

            Console.Write("Account Type: ")

 

            ' change colors and finish the Account Type Line

            Console.ForegroundColor = ConsoleColor.Blue

            Console.BackgroundColor = ConsoleColor.Black

            Console.Write(Space(2) + a.TypeOfAccount.ToString() +

            Environment.NewLine)

 

            ' check the balance to see if the account

            ' holder is in the red

            If a.Balance < 0 Then

 

                ' set the colors to write the title portion

                ' of the line

                Console.ForegroundColor = ConsoleColor.White

                Console.BackgroundColor = ConsoleColor.Black

                Console.Write("Balance: ")

 

                ' the account holder is in debt so show

                ' their negative balance in red

                Console.ForegroundColor = ConsoleColor.Red

                Console.BackgroundColor = ConsoleColor.Black

                Console.Write(Space(7) + a.Balance.ToString() + Environment.NewLine +

                Environment.NewLine)

 

            Else

 

                ' set the colors to write the title portion

                ' of the line

                Console.ForegroundColor = ConsoleColor.White

                Console.BackgroundColor = ConsoleColor.Black

                Console.Write("Balance: ")

 

                ' the account holder has a positive balance

                ' so show their balance in green

                Console.ForegroundColor = ConsoleColor.Green

                Console.BackgroundColor = ConsoleColor.Black

                Console.Write(Space(7) + a.Balance.ToString() + Environment.NewLine +

                Environment.NewLine)

            End If 

        Next

The main function wraps up by beeping one time and then using a Console Read call to pause the display and allow the user to read it.

       ' beep on completion

        Beep()

 

        ' wait for the user to read the information

        Console.Read()

 

    End Sub

 

The rest of the code is merely used to generate some fake data for display purposes.  The remainder of the code is as follows:

 

    ''' <summary>

    ''' This function creates a group of phony account

    ''' information so we have something to display

    ''' </summary>

    ''' <returns></returns>

    Public Function CreateAccounts() As List(Of Accounts)

 

        '' create a typed list to contain

        '' account information

        Dim list As New List(Of Accounts)()

 

        '' create and populate an account

        '' and then add it to the list

        Dim acct1 As New Accounts()

        acct1.FirstName = "William"

        acct1.MiddleName = "Alexander"

        acct1.LastName = "Carson"

        acct1.TypeOfAccount = Accounts.AccountType.Checking

        acct1.Balance = 121.5

        list.Add(acct1)

 

        '' create and populate an account

        '' and then add it to the list

        Dim acct2 As New Accounts()

        acct2.FirstName = "Barney"

        acct2.MiddleName = "Hubert"

        acct2.LastName = "Fortner"

        acct2.TypeOfAccount = Accounts.AccountType.Checking

        acct2.Balance = 1066.33

        list.Add(acct2)

 

        '' create and populate an account

        '' and then add it to the list

        Dim acct3 As New Accounts()

        acct3.FirstName = "Julia"

        acct3.MiddleName = "Mildred"

        acct3.LastName = "Daniels"

        acct3.TypeOfAccount = Accounts.AccountType.Savings

        acct3.Balance = 3397.58

        list.Add(acct3)

 

        '' create and populate an account

        '' and then add it to the list

        Dim acct4 As New Accounts()

        acct4.FirstName = "Alvin"

        acct4.MiddleName = "Micheal"

        acct4.LastName = "Bixby"

        acct4.TypeOfAccount = Accounts.AccountType.Checking

        acct4.Balance = -33.77

        list.Add(acct4)

 

        '' create and populate an account

        '' and then add it to the list

        Dim acct5 As New Accounts()

        acct5.FirstName = "Boris"

        acct5.MiddleName = "Winston"

        acct5.LastName = "Carloff"

        acct5.TypeOfAccount = Accounts.AccountType.Christmas

        acct5.Balance = 14551.52

        list.Add(acct5)

 

        '' create and populate an account

        '' and then add it to the list

        Dim acct6 As New Accounts()

        acct6.FirstName = "Debra"

        acct6.MiddleName = "Michelle"

        acct6.LastName = "Silvera"

        acct6.TypeOfAccount = Accounts.AccountType.Savings

        acct6.Balance = 936.93

        list.Add(acct6)

 

        '' create and populate an account

        '' and then add it to the list

        Dim acct7 As New Accounts()

        acct7.FirstName = "Camden"

        acct7.MiddleName = "Alphonse"

        acct7.LastName = "Villalobos"

        acct7.TypeOfAccount = Accounts.AccountType.Checking

        acct7.Balance = -71.29

        list.Add(acct7)

 

        '' create and populate an account

        '' and then add it to the list

        Dim acct8 As New Accounts()

        acct8.FirstName = "Drake"

        acct8.MiddleName = "Duk"

        acct8.LastName = "Mallard"

        acct8.TypeOfAccount = Accounts.AccountType.Christmas

        acct8.Balance = 815.18

        list.Add(acct8)

 

        '' create and populate an account

        '' and then add it to the list

        Dim acct9 As New Accounts()

        acct9.FirstName = "Talbert"

        acct9.MiddleName = "Daz"

        acct9.LastName = "Yatz"

        acct9.TypeOfAccount = Accounts.AccountType.Savings

        acct9.Balance = 14.21

        list.Add(acct9)

 

        '' create and populate an account

        '' and then add it to the list

        Dim acct10 As New Accounts()

        acct10.FirstName = "Miaxwif"

        acct10.MiddleName = "Isa"

        acct10.LastName = "Nidmare"

        acct10.TypeOfAccount = Accounts.AccountType.Checking

        acct10.Balance = -19697.33

        list.Add(acct10)

 

        '' return the list of dummy data to the caller

        Return list

 

    End Function 

 

    ''' <summary>

    ''' A class used to contain phony account information

    ''' </summary>

    Public Class Accounts

 

        ' set up an enumeration to

        ' define the possible account

        ' types

        Public Enum AccountType

            Checking

            Savings

            Christmas

        End Enum 

 

        ' private member variables

        Private mFirstName As String

        Private mMiddleName As String

        Private mLastName As String

        Private mAcctType As AccountType

        Private mBalance As Decimal 

 

        ' default constructor

        Public Sub New()

 

        End Sub 

 

        ' properties

 

        Public Property FirstName() As String

            Get

                Return mFirstName

            End Get

            Set(ByVal value As String)

                mFirstName = value

            End Set

        End Property 

 

        Public Property MiddleName() As String

            Get

                Return mMiddleName

            End Get

            Set(ByVal value As String)

                mMiddleName = value

            End Set

        End Property 

 

        Public Property LastName() As String

            Get

                Return mLastName

            End Get

            Set(ByVal value As String)

                mLastName = value

            End Set

        End Property 

 

        Public Property TypeOfAccount() As AccountType

            Get

                Return mAcctType

            End Get

            Set(ByVal value As AccountType)

                mAcctType = value

            End Set

        End Property 

 

        Public Property Balance() As Decimal

            Get

                Return mBalance

            End Get

            Set(ByVal value As Decimal)

                mBalance = value

            End Set

        End Property 

 

    End Class 

End Module

Summary

The article demonstrates an approach to applying color to a simple console mode application.  Using the Console class Foreground and Background color properties in conjunction with the Console Colors, it is possible to apply color to the display in its entirety, or by word or word, or even by individual letters.  Given the limitations for display within the context of a standard console mode application, color may be a useful tool or highlighting certain areas of the output, or to make the output more readable by using color to help break out certain areas of the display.


Similar Articles