字符串构建器对象的C#前景色

时间:2017-10-20 21:34:12

标签: c# console console-application

我正在使用Visual Studio中的C#控制台项目,我想更改StringBuilder对象的一部分的颜色。我无法使用WriteLine立即将该部分代码写入控制台,因为它是较大段落的一部分。因此,Is it possible to write to the console in colour in .NET?中提供的解决方案(在WriteLine语句之间使用ConsoleColor.Cyan语句)不适用。

这是我目前的方法:

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.");
        }
    }
}  

我希望部分item.InitialText以另一种颜色出现,比如洋红色。此颜色与文本的其余部分不同。我可以将item.InitialText拆分成我想要的字符串,但是如何使其中一个字符串变成不同的颜色呢?

我查看了几个资源,但它们似乎都是用于富文本框或其他图形界面,而不是控制台输出。

0 个答案:

没有答案