Color different parts of a RichTextBox string

前端 未结 9 785
暖寄归人
暖寄归人 2020-11-22 08:10

I\'m trying to color parts of a string to be appended to a RichTextBox. I have a string built from different strings.

string temp = \"[\" + DateTime.Now.ToSh         


        
9条回答
  •  深忆病人
    2020-11-22 08:33

    Selecting text as said from somebody, may the selection appear momentarily. In Windows Forms applications there is no other solutions for the problem, but today I found a bad, working, way to solve: you can put a PictureBox in overlapping to the RichtextBox with the screenshot of if, during the selection and the changing color or font, making it after reappear all, when the operation is complete.

    Code is here...

    //The PictureBox has to be invisible before this, at creation
    //tb variable is your RichTextBox
    //inputPreview variable is your PictureBox
    using (Graphics g = inputPreview.CreateGraphics())
    {
        Point loc = tb.PointToScreen(new Point(0, 0));
        g.CopyFromScreen(loc, loc, tb.Size);
        Point pt = tb.GetPositionFromCharIndex(tb.TextLength);
        g.FillRectangle(new SolidBrush(Color.Red), new Rectangle(pt.X, 0, 100, tb.Height));
    }
    inputPreview.Invalidate();
    inputPreview.Show();
    //Your code here (example: tb.Select(...); tb.SelectionColor = ...;)
    inputPreview.Hide();
    

    Better is to use WPF; this solution isn't perfect, but for Winform it works.

提交回复
热议问题