简体   繁体   English

在Richtextbox中更改线条的颜色

[英]Change the color of a line in Richtextbox

I want the second line, change to another color. 我要第二行,更改为另一种颜色。

My code is as follows : 我的代码如下:

 private void WindowLoaded(object sender, RoutedEventArgs e)
    {
        UpdateRTBOnDifferentThread("stackoverflow.com" + Environment.NewLine+"stackoverflow.org" + Environment.NewLine);
    }

    private void UpdateRTBOnDifferentThread(string message)
    {
        Dispatcher.Invoke(DispatcherPriority.Normal, new DispatcherOperationCallback(delegate
        {
            var newExternalParagraph = new Paragraph(new Run(message)) {Foreground = Brushes.Black};
            richTextBox1.Document.Blocks.Add(newExternalParagraph);
            return null;
        }), null);
    } 

The second line is: "stackoverflow.org" 第二行是:“ stackoverflow.org”

I want to change the color after the enter text 我要在输入文字后更改颜色

You can either use String.Split to break the lines inside your UpdateRTBOnDifferentThread method, or you can have this method accept an array of strings. 您可以使用String.Split来中断UpdateRTBOnDifferentThread方法中的行,也可以使此方法接受字符串数组。 Then you can iterate the array and add N Paragraph objects or N Run objects inside the same paragraph, and control the foreground of each string separately. 然后,您可以迭代该数组并在同一段落内添加N个Paragraph对象或N个Run对象,并分别控制每个字符串的前景。

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

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