I am working on a C# console project in Visual Studio, and I want to change the color of a portion of a StringBuilder
object. I can't write that part of the code to the console immediately with a WriteLine
because it's part of a larger paragraph. Therefore, the solution presented in Is it possible to write to the console in colour in .NET? (using WriteLine
statements between ConsoleColor.Cyan
statements) is not applicable.
This is my current method:
internal void ShowItems(StringBuilder builder, Player player)
{
foreach (Item item in Program.items.Values)
{
if (item.CurrentLocation == player.CurrentLocation.Name && item.CurrentLocation == item.InitialLocation)
{
builder.Append(item.InitialText);
}
else if (item.CurrentLocation == player.CurrentLocation.Name && item.CurrentLocation != item.InitialLocation)
{
builder.Append($" You see the {item.DisplayName.ToUpper()} here.");
}
}
}
I want part of the item.InitialText
to appear in another color, say magenta. This color is different than the rest of the text. I can split item.InitialText
, into the strings I want, but how do I make one of those strings a different color?
I have looked in several resources, but they all seem to be for rich text boxes or other graphical interfaces, not console output.