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).
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):
Figure 2: Solution Explorer
As you can see from Figure 2; there is a single console mode application entitled, "ColorfulConsole" which contains only the Program.cs file. All of the code used in the example project is contained in the Program.cs file.
The Application (Program.cs).
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):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Following the imports, the namespace, class, and constructor are defined:
namespace ColorfulConsole
{
public class Program
{
Next up, the main function begins by created a typed list of accounts; the account class is nested in the Program.cs file and is used to capture some fake account information so that there is something to display.
public static void Main(string[] args)
{
// create some dummy data so we have
// something to display
List<Accounts> accts = 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("\n\n");
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
var q =
(from a in accts
orderby a.LastName ascending
select a).ToList<Accounts>();
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
foreach (Accounts 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("\t\t\t" + a.LastName + ", " + a.FirstName + " " +
a.MiddleName + "\n");
// 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("\t\t" + a.TypeOfAccount + "\n");
// check the balance to see if the account
// holder is in the red
if (a.Balance < 0)
{
// 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("\t\t" + a.Balance + "\n\n");
}
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("\t\t" + a.Balance + "\n\n");
}
}
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
Console.Write("\a");
// wait for the user to read the information
Console.Read();
}
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 static List<Accounts> CreateAccounts()
{
// create a typed list to contain
// account information
List<Accounts> list = new List<Accounts>();
// create and populate an account
// and then add it to the list
Accounts acct1 = new Accounts();
acct1.FirstName = "William";
acct1.MiddleName = "Alexander";
acct1.LastName = "Carson";
acct1.TypeOfAccount = Accounts.AccountType.Checking;
acct1.Balance = 121.50M;
list.Add(acct1);
// create and populate an account
// and then add it to the list
Accounts acct2 = new Accounts();
acct2.FirstName = "Barney";
acct2.MiddleName = "Hubert";
acct2.LastName = "Fortner";
acct2.TypeOfAccount = Accounts.AccountType.Checking;
acct2.Balance = 1066.33M;
list.Add(acct2);
// create and populate an account
// and then add it to the list
Accounts acct3 = new Accounts();
acct3.FirstName = "Julia";
acct3.MiddleName = "Mildred";
acct3.LastName = "Daniels";
acct3.TypeOfAccount = Accounts.AccountType.Savings;
acct3.Balance = 3397.58M;
list.Add(acct3);
// create and populate an account
// and then add it to the list
Accounts acct4 = new Accounts();
acct4.FirstName = "Alvin";
acct4.MiddleName = "Micheal";
acct4.LastName = "Bixby";
acct4.TypeOfAccount = Accounts.AccountType.Checking;
acct4.Balance = -33.77M;
list.Add(acct4);
// create and populate an account
// and then add it to the list
Accounts acct5 = new Accounts();
acct5.FirstName = "Boris";
acct5.MiddleName = "Winston";
acct5.LastName = "Carloff";
acct5.TypeOfAccount = Accounts.AccountType.Christmas;
acct5.Balance = 14551.52M;
list.Add(acct5);
// create and populate an account
// and then add it to the list
Accounts acct6 = new Accounts();
acct6.FirstName = "Debra";
acct6.MiddleName = "Michelle";
acct6.LastName = "Silvera";
acct6.TypeOfAccount = Accounts.AccountType.Savings;
acct6.Balance = 936.93M;
list.Add(acct6);
// create and populate an account
// and then add it to the list
Accounts acct7 = new Accounts();
acct7.FirstName = "Camden";
acct7.MiddleName = "Alphonse";
acct7.LastName = "Villalobos";
acct7.TypeOfAccount = Accounts.AccountType.Checking;
acct7.Balance = -71.29M;
list.Add(acct7);
// create and populate an account
// and then add it to the list
Accounts acct8 = new Accounts();
acct8.FirstName = "Drake";
acct8.MiddleName = "Duk";
acct8.LastName = "Mallard";
acct8.TypeOfAccount = Accounts.AccountType.Christmas;
acct8.Balance = 815.18M;
list.Add(acct8);
// create and populate an account
// and then add it to the list
Accounts acct9 = new Accounts();
acct9.FirstName = "Talbert";
acct9.MiddleName = "Daz";
acct9.LastName = "Yatz";
acct9.TypeOfAccount = Accounts.AccountType.Savings;
acct9.Balance = 14.21M;
list.Add(acct9);
// create and populate an account
// and then add it to the list
Accounts acct10 = new Accounts();
acct10.FirstName = "Miaxwif";
acct10.MiddleName = "Isa";
acct10.LastName = "Nidmare";
acct10.TypeOfAccount = Accounts.AccountType.Checking;
acct10.Balance = -19697.33M;
list.Add(acct10);
// return the list of dummy data to the caller
return list;
}
}
/// <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
}
// private member variables
private string mFirstName;
private string mMiddleName;
private string mLastName;
private AccountType mAcctType;
private decimal mBalance;
// default constructor
public Accounts()
{
}
// properties
public string FirstName
{
get
{
return mFirstName;
}
set
{
mFirstName = value;
}
}
public string MiddleName
{
get
{
return mMiddleName;
}
set
{
mMiddleName = value;
}
}
public string LastName
{
get
{
return mLastName;
}
set
{
mLastName = value;
}
}
public AccountType TypeOfAccount
{
get
{
return mAcctType;
}
set
{
mAcctType = value;
}
}
public decimal Balance
{
get
{
return mBalance;
}
set
{
mBalance = value;
}
}
}
}
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.