Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Console Text Foreground Color Changing in C#
WhatsApp
Abhay Shiravanthe
Jan 29
2016
2.1
k
0
0
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
Up Next
Console Text Foreground Color Changing in C#