TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Console Text Foreground Color Changing in C#
Abhay Shiravanthe
Jan 29
2016
Code
2
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
using
System;
namespace
console_color_on_demand
{
class
Console_Color
{
Type type =
typeof
(ConsoleColor);
string
sampleText;
string
color;
string
defaultColor =
"White"
;
public
Console_Color()
{
sampleText =
"Sample text here!"
;
color =
"White"
;
}
public
void
GetData()
{
Console.WriteLine(
"Enter a string!"
);
sampleText = Console.ReadLine();
Console.WriteLine(
"Choose a color:\n1.\tRed\n2.\tYellow\n3.\tDarkBlue\n4.\tDarkGreen\n5.\tDarkCyan\n6.\tDarkRed \n7.\tDarkMagenta\n8.\tDarkYellow\n9.\tGray\n10.\tDarkGray\n11.\tGreen\n12.\tCyan\n13.\tMagenta \n14.\tWhite (default)\n"
);
int
choice = Convert.ToInt32(Console.ReadLine());
GetColorTextData(choice);
Console.WriteLine(
"Do you want to make this color default?\n1.\tYes\n2.\tNo\n"
);
int
ans = Int32.Parse(Console.ReadLine());
if
(ans == 1)
{
defaultColor = color;
}
DisplayTextInColor();
defaultTextColor();
Console.WriteLine(
"Press Ctrl+C to exit"
);
Console.ReadKey();
Console.Clear();
}
void
defaultTextColor()
{
Console.ForegroundColor = (ConsoleColor) Enum.Parse(type, defaultColor);
}
void
DisplayTextInColor()
{
Console.ForegroundColor = (ConsoleColor) Enum.Parse(type, color);
Console.WriteLine(sampleText);
Console.ResetColor();
}
void
GetColorTextData(
int
choices)
{
switch
(choices)
{
case
1:
color =
"Red"
;
break
;
case
2:
color =
"Yellow"
;
break
;
case
3:
color =
"DarkBlue"
;
break
;
case
4:
color =
"DarkGreen"
;
break
;
case
5:
color =
"DarkCyan"
;
break
;
case
6:
color =
"DarkRed"
;
break
;
case
7:
color =
"DarkMagenta"
;
break
;
case
8:
color =
"DarkYellow"
;
break
;
case
9:
color =
"Gray"
;
break
;
case
10:
color =
"DarkGray"
;
break
;
case
11:
color =
"Green"
;
break
;
case
12:
color =
"Cyan"
;
break
;
case
13:
color =
"Red"
;
break
;
case
14:
color =
"Magenta"
;
break
;
default
:
color =
"White"
;
break
;
}
}
}
class
Program
{
static
void
Main(
string
[] args)
{
var c1 =
new
Console_Color();
while
(
true
)
{
c1.GetData();
}
}
}
}
C#
ConsoleColor
Foreground Color