2016-11-19 97 views
0

我有這樣的代碼片段: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)); 
} 

我怎樣才能使文本的一部分紅?

+0

我看不出這是這個問題的副本'http://stackoverflow.com/questions/39085397/highlighting-a-line-in-a-richtextbox1-line-number-a-因爲這個問題有一個不同的變量。如果我錯了,請嘗試以不同的方式解釋答案,因爲我不明白'http://stackoverflow.com/questions/39085397/highlighting-a-line-in-a-richtextbox1-line- number-a-variable' – TheGejr

回答

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"); 
+0

我已經寫了這個,但既沒有'SelectionColor'也沒有'AppendText'是一個藍色,我錯過了一個類或參考? 而文字不會改變它的顏色 – TheGejr