RichTextBox文本颜色为特定行

时间:2016-11-19 17:05:17

标签: c# richtextbox

我有这段代码:

if (newLocation.MonsterLivingHere != null)
{
    rtbMessages.Text += "You see a " + newLocation.MonsterLivingHere.Name + Environment.NewLine;
}

我希望" 你看到(怪物名称)"变红了。

我有几个地方我想要一条线为红色,但我在框中有很多其他文字我不想变成红色。例如,这段代码也需要是红色的:

if(_player.CurrentHitPoints <= 0)
{
    //Display a dead message
    rtbMessages.Text = " " + Environment.NewLine;
    rtbMessages.Text += "The " + _currentMonster.Name + " killed you." + Environment.NewLine;
    //Moves the player to "Home"
    MoveTo(World.LocationByID(World.LOCATION_ID_HOME));
}

如何才能让部分文字变成红色?

1 个答案:

答案 0 :(得分:0)

设置选择颜色,然后下一次插入将采用该颜色,例如

Color current = rtbMessages.SelectionColor;

rtbMessages.SelectionColor = Color.Red;
rtbMessages.AppendText("I am red" + Environment.NewLine);

rtbMessages.SelectionColor = Color.Blue;
rtbMessages.AppendText("I am blue" + Environment.NewLine);

rtbMessages.SelectionColor = current;
rtbMessages.AppendText("I am whatever colour was set before red");