简体   繁体   English

Richtextbox 在新文本前面加上颜色

[英]Richtextbox prepend new text with color

I have used a richtextbox to display logs in my WinForms.我使用富文本框在我的 WinForms 中显示日志。

Language used is C#.使用的语言是 C#。

The software is used for inserting data of Bank Branches and after start of new Branch I want to display a text with new color.该软件用于插入银行分行的数据,在新分行开始后,我想以新颜色显示文本。

I have seen the link Color different parts of a RichTextBox string and implemeted it successfully.我已经看到链接颜色 RichTextBox 字符串的不同部分并成功实现了它。

My problem is I want to prepend the new line instead of append.我的问题是我想添加新行而不是追加。 That is the new line will be displayed on top.即新行将显示在顶部。

I am able to do this by changing the code to box.Text=DateTime.Now.ToString("dd-MM-yyyy-hh-mm-ss") + ": " + text + box.Text我可以通过将代码更改为box.Text=DateTime.Now.ToString("dd-MM-yyyy-hh-mm-ss") + ": " + text + box.Text

But the color is changing for the entire text.但是整个文本的颜色正在改变。

This is the procedure used for append这是用于追加的过程

            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;

This is what I have done:这就是我所做的:

            box.Text=DateTime.Now.ToString("dd-MM-yyyy-hh-mm-ss") + ": " + text + box.text;
        box.SelectionStart = 0;
        box.SelectionLength = text.length;
        box.SelectionColor = color;

But this is not working.但这是行不通的。

1) Never directly change the Text property of an already formatted RichtTextBox 1)切勿直接更改已格式化的RichtTextBoxText属性

2) To append use the RTB.AppendText function 2) 要追加使用RTB.AppendText函数

3) To insert at any other position p , including the beginning use this: 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 

Now you can add the formatting you want:现在您可以添加所需的格式:

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..
...
    // Prepend, normal on first line, rest of lines gray italic

    private void PrependToRichTextbox(RichTextBox rt, string msg)
    {
        rt.SelectionStart = 0;
        rt.SelectionLength = 0;
        rt.SelectedText = msg + Environment.NewLine;

        int linecount = 0;
        foreach (var line in rt.Lines)
        {
            rt.Select(rt.GetFirstCharIndexFromLine(linecount), line.Length);
            rt.SelectionColor = linecount == 0 ? Color.Black : Color.Gray;
            Font currentFont = rt.SelectionFont;
            FontStyle newFontStyle;
            newFontStyle = linecount == 0 ? FontStyle.Regular : FontStyle.Italic;
            rt.SelectionFont = new Font(
               currentFont.FontFamily,
               currentFont.Size,
               newFontStyle
            );
            linecount++;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM