简体   繁体   中英

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"

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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