Introduction
So, you need to 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;
namespace ColorfulConsole
{
public class Program
{
// Your code goes here
}
}
public static void Main(string[] args)
{
// create some dummy data so we have
// something to display
List<Accounts> accts = CreateAccounts();
// Your code continues...
}
er, 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 the 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 the 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 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");
}
}
// 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.