日期:2014-05-18  浏览次数:20793 次

如何让richtextbox当前输出信息为红色,之前的为黑色
如何让每次输出时,只有当前输出的为红色,之前输出的都是黑色

------解决方案--------------------
void Add2RichEditBox(RichEditBox r,string p)
{
int ori_len = r.Length;
r.Select(0,r.Length);
r.SelectColor=Color.Black;


r.AppendText(p);
int now_len = r.Length;
r.Select(ori_len ,now_len);
r.SelectColor=Color.Red;
}
------解决方案--------------------
C# code
 private void button1_Click(object sender, EventArgs e)
 {
     // 将以前的文本变为黑色
     this.richTextBox1.SelectAll();
     this.richTextBox1.SelectionColor = Color.Black;

     int begin = this.richTextBox1.Text.Length - 1;
     if (begin < 0) begin = 0;

     // 添加的文本内容
     string addToRichtextbox = "add @ " + DateTime.Now.ToString("yyyyMMddHHmmss.fff");
     this.richTextBox1.Text += addToRichtextbox + Environment.NewLine;

     int end = this.richTextBox1.Text.Length - 1;
     this.richTextBox1.Select(begin, end - begin+1);
     // 将新加入的文本变为红色
     this.richTextBox1.SelectionColor = Color.Red;
 }