Click here to Skip to main content
15,891,925 members
Articles / Programming Languages / Visual Basic
Article

Get colors in your VB.NET console application

Rate me:
Please Sign up or sign in to vote.
4.54/5 (14 votes)
17 Jul 2003 116.9K   985   31   10
Use Windows APIs to write a console tool module, able to change your console background and foreground colors.

Sample Image - color_console.jpg

Introduction

How can i use colors to make my console application look pretty with VB.NET ? I was writing a remoting server when this idea came ... So i've made this little module ! It was not really difficult to do but i think it could save your time ;)

How to use it ?

You can either change background and foregroud ...

VB.NET
ConsoleTools.SetConsoleColors(ConsoleColor.Green, ConsoleColor.DarkBlue)

... or just foreground :

VB.NET
ConsoleTools.SetConsoleColors(ConsoleColor.Pink) 

How does it run ?

I just use kernel api's SetConsoleTextAttribute (to change colors) and GetStHandle (to retrieve console output handles). An enumeration has been created to have bases colors codes : enumeration values must be directly used to change foreground color, and enumeration values * 16 (mask) must be used to change foreground color.

So you just have now to use SetConsoleColors function (which use kernel's api) to change your console colors ...

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
France France
Actually software architect for a little company based in France (Rennes), I'm specialized in distributed applications. I work a lot with .net remoting but don't have enough time to write articles about it ...Frown | :(

Comments and Discussions

 
Questionthanks for the jazz up Pin
Kobracode27-Oct-14 13:32
Kobracode27-Oct-14 13:32 
Generalthanks! Pin
simply_joe23-Mar-08 15:41
simply_joe23-Mar-08 15:41 
GeneralSmall bug Pin
PhiL6667-Dec-06 15:25
PhiL6667-Dec-06 15:25 
GeneralRe: Small bug Pin
madestro13-Dec-06 7:14
madestro13-Dec-06 7:14 
SuggestionRe: Small bug Pin
Wilfredo Gumersindo6-Sep-11 5:32
Wilfredo Gumersindo6-Sep-11 5:32 
If you want to change only the foreground color, use Console.Foregroundcolor, and not this function (I see the point on the one that changes both, but this one...)
GeneralAstonishing Pin
N. Altamura19-Jan-06 21:53
N. Altamura19-Jan-06 21:53 
GeneralCreata, but litle Error using Option Strict Pin
heck-17-Nov-05 21:18
heck-17-Nov-05 21:18 
GeneralConsole Code Colors Pin
brichard23-Jun-04 11:14
brichard23-Jun-04 11:14 
QuestionIcons? Pin
Uwe Keim18-Jul-03 0:55
sitebuilderUwe Keim18-Jul-03 0:55 
AnswerRe: Icons? Pin
Roger Alsing18-Aug-03 22:13
Roger Alsing18-Aug-03 22:13 

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.