一个关于RichTextBox的有关问题,可能很简单,但是小弟我搞不懂
一个关于RichTextBox的问题,可能很简单,但是我搞不懂~

这段代码,为什么字母b的样式会跟字母a一样的?
就算我继续搞n个字母,那么前n-1个字母的样式都会跟第一个的一样,只有最后一个字母的样式跟想要的样式设定成一样的。实在不解,估计是用错Select方法了……
这样是没问题了:

可是,我的文本在程序运行过程中要不断被更新啊,那该怎么办好呢?
------解决方案--------------------
哪你就可以等文本更新好了在重新执行颜色设置代码块,是否可以解决你的问题。
------解决方案--------------------
试试看加上一个
this.rtbScreen.Text += "a";
Application.DoEvents();
this.rtbScreen.Select(0, 1);
this.rtbScreen.SelectionBackColor = Color.Red;
this.rtbScreen.SelectionColor = Color.Cyan;
this.rtbScreen.Text += "b";
Application.DoEvents();
this.rtbScreen.Select(1, 1);
this.rtbScreen.SelectionBackColor = Color.Green;
this.rtbScreen.SelectionColor = Color.Yellow;
this.rtbScreen.Text += "c";
Application.DoEvents();
this.rtbScreen.Select(2, 1);
this.rtbScreen.Text += "a";
this.rtbScreen.Select(0, 1);
this.rtbScreen.SelectionBackColor = Color.Red;
this.rtbScreen.SelectionColor = Color.Cyan;
this.rtbScreen.Text += "b";
this.rtbScreen.Select(1, 1);
this.rtbScreen.SelectionBackColor = Color.Green;
this.rtbScreen.SelectionColor = Color.Yellow;
this.rtbScreen.Text += "c";
this.rtbScreen.Select(2, 1);
this.rtbScreen.SelectionBackColor = Color.Blue;
this.rtbScreen.SelectionColor = Color.White;
这段代码,为什么字母b的样式会跟字母a一样的?
就算我继续搞n个字母,那么前n-1个字母的样式都会跟第一个的一样,只有最后一个字母的样式跟想要的样式设定成一样的。实在不解,估计是用错Select方法了……
这样是没问题了:
this.rtbScreen.Text += "abc";
this.rtbScreen.Select(0, 1);
this.rtbScreen.SelectionBackColor = Color.Red;
this.rtbScreen.SelectionColor = Color.Cyan;
//this.rtbScreen.Text += "b";
this.rtbScreen.Select(1, 1);
this.rtbScreen.SelectionBackColor = Color.Green;
this.rtbScreen.SelectionColor = Color.Yellow;
//this.rtbScreen.Text += "c";
this.rtbScreen.Select(2, 1);
this.rtbScreen.SelectionBackColor = Color.Blue;
this.rtbScreen.SelectionColor = Color.White;
可是,我的文本在程序运行过程中要不断被更新啊,那该怎么办好呢?
RichTextBox
文本
------解决方案--------------------
哪你就可以等文本更新好了在重新执行颜色设置代码块,是否可以解决你的问题。
------解决方案--------------------
试试看加上一个
this.rtbScreen.Text += "a";
Application.DoEvents();
this.rtbScreen.Select(0, 1);
this.rtbScreen.SelectionBackColor = Color.Red;
this.rtbScreen.SelectionColor = Color.Cyan;
this.rtbScreen.Text += "b";
Application.DoEvents();
this.rtbScreen.Select(1, 1);
this.rtbScreen.SelectionBackColor = Color.Green;
this.rtbScreen.SelectionColor = Color.Yellow;
this.rtbScreen.Text += "c";
Application.DoEvents();
this.rtbScreen.Select(2, 1);