问题描述:
我想向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());
}
我想向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());
}
{
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;
}
}
this.richTextBox1.AppendText(Msg);
int startpos=this.richTextBox1.Text.Length-Msg.Length;
this.serverlogt.Select(startpos,Msg.Length);
this.richTextBox1.SelectionColor=color;