我已经使用Richtextbox在WinForms中显示日志。
使用的语言是C#。
该软件用于插入银行分行的数据,在新的分行启动后,我想显示具有新颜色的文本。
我已经看到了RichTextBox字符串的链接Color的不同部分,并成功实现了它。
我的问题是我想在新行之前添加而不是追加。也就是说,新行将显示在顶部。
我可以通过将代码更改为 box.Text=DateTime.Now.ToString("dd-MM-yyyy-hh-mm-ss") + ": " + text + box.Text
但是整个文本的颜色都在变化。
这是用于追加的过程
box.SelectionStart = box.TextLength;
box.SelectionLength = 0;
box.SelectionColor = color;
box.AppendText(DateTime.Now.ToString("dd-MM-yyyy-hh-mm-ss") + ": " + text);
box.SelectionColor = box.ForeColor;
这是我所做的:
box.Text=DateTime.Now.ToString("dd-MM-yyyy-hh-mm-ss") + ": " + text + box.text;
box.SelectionStart = 0;
box.SelectionLength = text.length;
box.SelectionColor = color;
但这是行不通的。
1)切勿直接更改Text
已格式化的属性RichtTextBox
2)追加使用RTB.AppendText
功能
3)要插入其他任何位置p
(包括开头),请使用以下命令:
rtb.SelectionStart = s; // set the cursor to the target position
rtb.Selection.Length = 0; // nothing selected, yet
rtb.SelectedText = yourNewText; // this inserts the new text
现在,您可以添加所需的格式:
rtb.SelectionStart = s; // now we prepare the new formatting..
rtb.SelectionLength = yourNewText.Length; //.. by selecting the text
rtb.SelectionColor = Color.Blue; // and/or whatever you want to do..
...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句