List strWarn=new List();//定义一个泛型,可以自由的添加字段
private void WarnInfo(string str)
{
strWarn.Add(str);
for (int i = 0; i < strWarn.Count; i++)
{
int lenth = richTextBox1.Text.Length;
richTextBox1.AppendText(strWarn[i]);
if (richTextBox1.Lines[i].Contains("一级警告"))
{
for (int j = 0; strWarn[i][j] != '\n'; j++)//对于这一行来说\n是结尾
{
richTextBox1.Select(lenth + j, 1);//一个一个的选择然后改变颜色
richTextBox1.SelectionColor = Color.Red;
}
}
if (richTextBox1.Lines[i].Contains("二级警告"))
{
for (int j = 0; strWarn[i][j] != '\n'; j++)
{
richTextBox1.Select(lenth + j, 1);
richTextBox1.SelectionColor = Color.Orange;
}
}
}
}
当需要滚动显示出来的信息都是显示最后的信息可以附加:
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
richTextBox1.SelectionStart = richTextBox1.TextLength;
// Scrolls the contents of the control to the current caret position.
richTextBox1.ScrollToCaret();
}