我想让RichTextBox中特定的文本有特定的颜色,写了如下代码:
if(...)
{
this.RichTextBox1.SelectionColor = Color.Red;
this.RichTextBox1.AppendText("");
this.RichTextBox1.AppendText( "要添加的文本");
this.RichTextBox1.SelectionColor = Color.Black;
}
else
{
//debug1
this.RichTextBox1.Text = noteContent;
//debug2
}
在运行完if里的语句块时,点击鼠标后执行else里面的程序段时字体还是红色.
更诡异的事,我加入断点debug时,在debug1处,this.RichTextBox1.SelectionColor还是black,而到了debug2处就自动变为Red了.
请高人指定,谢谢!着急ing....
if(...)
{
this.RichTextBox1.SelectionColor = Color.Red;
this.RichTextBox1.AppendText("");
this.RichTextBox1.AppendText( "要添加的文本");
this.RichTextBox1.SelectionColor = Color.Black;
}
else
{
//debug1
this.RichTextBox1.Text = noteContent;
//debug2
}
在运行完if里的语句块时,点击鼠标后执行else里面的程序段时字体还是红色.
更诡异的事,我加入断点debug时,在debug1处,this.RichTextBox1.SelectionColor还是black,而到了debug2处就自动变为Red了.
请高人指定,谢谢!着急ing....
可是还是没有人回答,郁闷ing!
期待有人解答,谢!
我看到很多网上让RichTextBox改变字体颜色的方法都是用的这一种,就是不知道出了什么问题,很奇怪
{
this.noteContentText.Text = "";
String noteContent = e.Node.ToolTipText.ToString();
if (nc.hasRows(noteID,csID))//如果数据库中存在记录,存数据库中取出
{
noteCodeList = nc.listByNoteIdAndCSId(noteID,csID);//noteCodeList是一个ArrayList
foreach (NoteCode ncObj in noteCodeList)
{
int start = this.noteContentText.Text.Length;
int length = ncObj.AttContent.Length;
if (ncObj.AttCodeID != 0)//如果已经数据库此属性已经被赋值
{
this.noteContentText.SelectionColor = Color.Red;
this.noteContentText.AppendText("");
this.noteContentText.AppendText( ncObj.AttContent + "\n");
this.noteContentText.SelectionColor = Color.Black;
}
else
{
this.noteContentText.AppendText( ncObj.AttContent + "\n");
}
}
}
else
{
this.noteContentText.Text = noteContent;
} }谢谢
如果点击node1时,不进入第一个if,直接进入
else
{
this.noteContentText.Text = noteContent;
}
可以看到此时RichTextBox中的颜色正常为黑色,(正确)
然后再点击node2执行了更改颜色的操作,此时我需要的特定文本的颜色为红色(正确)
我再点击node1还是直接进入else但此时RichTextBox中的颜色为红色(错误)
是否这个RichTextBox本来的SelectionColor默认设了Red?应该不是,因为我如果点击没有满足if条件的结点,文本还是黑色,是正确的
但是点击了满足if条件的节点,再点击原来的节点,文本都变成红色了
==============================================
Knight94(愚翁)
我的foreach是要把与节点相关的数据库中的所有记录读出来,一个节点的所有记录分满足 if (ncObj.AttCodeID != 0)//和else两种情况,仅有if的时候才将文本变为红色