0
配置:格式的文本颜色表格
- 的Windows 7
- .NET 3.5
- 的Visual Studio 2008
摘要:通过发送的话到RTB一个for循环;根据它们的内容对它们进行格式化(绿色显示“OK”,红色显示“失败”)。
代码:
for (int i = 0; i < inputValues.Length; i++)
{
//checking if the value is OK or not
string answer = functionReturningString(inputValues[i], referenceValue);
textBox4.Text += answer; //and sending the result string to text box
}
现在我只是想选择最后添加字符串,并根据其内容进行格式化。
textBox4.SelectAll();
textBox4.Select(textBox4.SelectedText.Length - answer.Length, answer.Length);
if (answer == "OK")
{
textBox4.SelectionColor = Color.Green;
} else {
textBox4.SelectionColor = Color.Red;
}
textBox4.Refresh();//I want to see every value as soon as added
textBox4.Text += "\r\n"; //adding space between words
至于结果,它最终将对rtb中的所有单词使用“SelectionColor”。
问:如何确保先前格式化的单词不会再改变颜色?
更新:建议的解决方案也不起作用。单词将以正确的颜色显示(首先)。然后添加下一个单词并且整个框的颜色改变。
http://stackoverflow.com/questions/2527700/change-color-of-text-within-a-winforms-richtextbox/2531177#2531177 –
@Ivan stoev我需要删除rtb.refresh(),如果我使用你建议的代码? – Avigrail
我不确定,为什么不尝试看看是否需要。 –