问题描述:
我想向richtextbox中增加一行行数据,但每行数据我都要控制其颜色,示意代码如下,但问题是:我用MessageBox暂停时,可以看到最后一行总是可以被成功更改,但是其前面的每行颜色都变成了一样的,也就是最初在ShowLog("abc\n",Color.Red)中定义的红色,怎么也无法实现每行颜色不同。不知有无高手帮忙解决,谢谢,在线等,解决即可结贴
代码示意:
private void button1_Click(object sender, System.EventArgs e)
{
  ShowLog("abc\n",Color.Red); 
  ShowLog("def\n",Color.Blue); 
  ShowLog("ghi\n",Color.Green); 
  ShowLog("jkl\n",Color.CadetBlue); 
}private void ShowLog(string Msg,Color color)
{
  this.richTextBox1.Text+=Msg;
  int startpos=this.richTextBox1.Text.Length-Msg.Length;
  this.serverlogt.Select(startpos,Msg.Length); 
  this.richTextBox1.SelectionColor=color; 
  //MessageBox.Show(this,this.serverlogt.SelectedText+color.ToString());  
}

解决方案 »

  1.   

    public void ChangeMySelectionColor()
    {
       ColorDialog colorDialog1 = new ColorDialog();   // Set the initial color of the dialog to the current text color.
       colorDialog1.Color = richTextBox1.SelectionColor;   // Determine if the user clicked OK in the dialog and that the color has changed.
       if(colorDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK && 
          colorDialog1.Color != richTextBox1.SelectionColor)
       {
          // Change the selection color to the user specified color.
          richTextBox1.SelectionColor = colorDialog1.Color;
       }
    }
      

  2.   

    你那个方法简单,我也会,但现在我要通过代码控制,不需要colordialog
      

  3.   

    //this.richTextBox1.Text+=Msg;
    this.richTextBox1.AppendText(Msg);
      int startpos=this.richTextBox1.Text.Length-Msg.Length;
      this.serverlogt.Select(startpos,Msg.Length); 
      this.richTextBox1.SelectionColor=color; 
      

  4.   

    要实现用代码控制不同的颜色,只能用rtf文本
      

  5.   

    rtf文本?难道我这种方法不属于rtf文本?如何实现转化呢?为什么我最后添加的一行总是能实现颜色的改变呢?
      

  6.   

    JadyWang(最近不矛盾了) 您的方法也是正确的,我前面怎么没看到呢?呵呵,少给您70分,下次有机会再补上