Click here to Skip to main content
15,910,877 members
Articles / Programming Languages / C#

Using ANSI Colors within .NET

Rate me:
Please Sign up or sign in to vote.
2.57/5 (6 votes)
28 Mar 2008CPOL2 min read 46.4K   634   13   8
This article will teach you how to use ANSI colors using .NET
Screenshot

Introduction

This article will give you some insight on using Ansi Colors within your projects. The main use that I have used this code for is when I build a Windows service. Usually I add in a tcplistener so that multiple users can telnet to the server and command it. Adding colors to the output makes viewing information easier on the eyes. This code would also be useful for anyone interested in creating a MUD.

This code does NOT work from the command prompt. If anyone is interested in an article dealing with that, please write a comment and let me know.

Background

This article will require you to have very little programming knowledge. Knowing how to use a collection and the TcpListener class will help you out.

Using the Code

The source code is very simple to use. I have created a helper class called AnsiColor. This class contains the static method AnsiColor.Colorize that will accept a string and parse out any colors that you have entered. Here is a very simple sample for using the code:

C#
string colorString = AnsiColor.Colorize
  ( "{red}This is red!\r\n{blue}This is blue\r\n{!red}With a red background.\r\n" );

In the example project, I have added a small TcpListener so that you may test the demo by using the telnet program. Simply start the demo and telnet to localhost port 5484. I will not be explaining anything about TcpListener as there are many great references here on the interweb.

Because you can highlight both foreground and background, custom effects can be created. Here is a function that will create a simple progress bar:

C#
public static string ProgressBarDemo 
	( int percent, int width, string colorCode )
{
	// Our max size
	int MaxSize = 10;

	// Make sure our percent is valid. If it is not force it to zero
	if ( percent < 0 || percent > 100 ) percent = 0;

	// Calculate how many colored blocks we should have
	int blocks = ( int ) ( ( percent / 100f ) * width );

	// Default our progress bar to start with a red background
	string progressBar = "{" + colorCode;
	// Add our blocks
	for ( int i = 0; i < blocks; i++ )
		// Append our space
		progressBar += ' ';

	// Back to normal
	progressBar += "{reset}";

	// Add our blocks
	for ( int i = 0; i < width - blocks; i++ )
		// Append our space
		progressBar += ' ';

	// Append our final char
	progressBar += '}';

	// Return our progress bar
	return ( "Progress Bar: " + Colorize ( progressBar ) + "\r\n" );
} // End of ProgressBarDemo

This function exists within the AnsiColor class, as well as a ColorDemo function that loops through all of the codes and displays the results.

Points of Interest

When using the .NET string formatting functions (string.Format), you have to beware of your curly brackets { & }s. For example, take this code:

C#
string.Format ( "{red}{0}{reset}", "This is red text!" );

This will give you compiler errors because when formatting text, the curly brackets are special characters. The proper syntax would actually be:

C#
string.Format ( "{{red}}{0}{{reset}}", "This is red text!" );

References

History

  • 1.0 - First release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO Compilr
Canada Canada
Kyle Hankinson is the Owner and Operator of the Online Compiler website Compilr (compilr.com). He enjoys programming both as a hobby and a job.

Comments and Discussions

 
Generalusing Console instead of NetworkStream Pin
JackRnl22-Jul-08 6:22
JackRnl22-Jul-08 6:22 
Generaltelnet client Pin
PIEBALDconsult28-Mar-08 11:46
mvePIEBALDconsult28-Mar-08 11:46 
GeneralRe: telnet client Pin
zenox28-Mar-08 14:18
zenox28-Mar-08 14:18 
GeneralConsole Pin
PIEBALDconsult28-Mar-08 11:01
mvePIEBALDconsult28-Mar-08 11:01 
GeneralRe: Console Pin
zenox28-Mar-08 14:20
zenox28-Mar-08 14:20 
GeneralDEVICE=ANSI.SYS; Pin
evolved28-Mar-08 8:26
evolved28-Mar-08 8:26 
GeneralRe: DEVICE=ANSI.SYS; Pin
zenox28-Mar-08 14:19
zenox28-Mar-08 14:19 
JokeOld school Pin
dethtroll28-Mar-08 7:36
dethtroll28-Mar-08 7:36 
Nice Smile | :)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.