Hello,

I am in a process of making an adventure text-based game and I was wondering if it's possible to change the text color on the go. What I mean is that normally, your text would be white, but when you advance in the story (e.g. advance on lines), the old lines would get grey color.

Is it even possible to change the color of the lines already printed in console?

Thanks for any help.

This code snippet should help you do what you want.

int x = Console.CursorLeft;
int y = Console.CursorTop;
ConsoleColor initColour = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("This is white Text");
Console.ForegroundColor = initColour;
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
Console.SetCursorPosition(x,y);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("This is yellow Text");
Console.ForegroundColor = initColour;
Console.WriteLine("Press any key to continue...");
Console.ReadKey();

Edited 5 Years Ago by nick.crane: Modified to use SetCursorPosition

I just realized I posted it to the wrong forum by mistake. I write in C++ not in C# .. non the less, isn't this VC++ code? I am kinda new to whole this, correct me, if I am wrong. Thanks anyway

In a CLR Console Application my original code snippet looks like this.

int x = Console::CursorLeft;
int y = Console::CursorTop;
ConsoleColor initColour = Console::ForegroundColor;
Console::ForegroundColor = ConsoleColor::White;
Console::WriteLine("This is white Text");
Console::ForegroundColor = initColour;
Console::WriteLine("Press any key to continue...");
Console::ReadKey();
Console::SetCursorPosition(x,y);
Console::ForegroundColor = ConsoleColor::Yellow;
Console::WriteLine("This is yellow Text");
Console::ForegroundColor = initColour;
Console::WriteLine("Press any key to continue...");
Console::ReadKey();

And works just the same.

This article has been dead for over six months. Start a new discussion instead.