How to make a public static method to be able to color part of ta ext in a wpf richTextBox ?

Chocolade 516 Reputation points
2022-05-07T15:09:55.033+00:00

In MainWindow.xaml.cs i used this way to color part of a text each time i needed :

TextRange rangeOfText1 = new TextRange(RichTextBoxLogger.Document.ContentEnd, RichTextBoxLogger.Document.ContentEnd);  
rangeOfText1.Text = "\rWatch Event Ended At : " + DateTime.Now.ToString();  
rangeOfText1.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);  
rangeOfText1.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);  

If for example in the rcihTextBox next time i will add a line and in the line the word "Watch" or the string "\rWatch Event Ended At : " + DateTime.Now.ToString(); will be in it it will color the line to Red.

but what i'm trying to do is instead writing all this code each time to color a different line that will add to the richTextBox using AppendText to make some easier method.

This is a new class i created with a method i want to use in the MainWindow.xaml.cs

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Text.RegularExpressions;  
using System.Threading.Tasks;  
using System.Windows.Controls;  
using System.Windows.Documents;  
using System.Windows.Media;  
  
namespace WPF_File_System_WAtcher  
{  
    class ColorRichTextBoxText  
    {  
        public static void HighlightText(RichTextBox rtb, string textToColor, Color color)  
        {  
            TextRange rangeOfText1 = new TextRange(rtb.Document.ContentEnd, rtb.Document.ContentEnd);  
            rangeOfText1.Text = textToColor;  
            rangeOfText1.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);  
            rangeOfText1.ApplyPropertyValue(TextElement.FontWeightProperty, System.Windows.FontWeights.Bold);  
        }  
    }  
}  
  

First i'm not sure if this is the right way to do it if this method is what i need and the second problem is how to use the Color so i can do something like :

private void Fsw_Deleted(object sender, FileSystemEventArgs e)  
        {  
            ColorRichTextBoxText.HighlightText(RichTextBoxLogger, "File Deleted At : "  
                + DateTime.Now.ToString(), Color.Red);  
        }  

So it will add a new line to the richTextBox in color red.
The problem is that in the MainWindow.xaml.cs i can't type Color.Red because i don't have System.Drawing in use and if i will use the System.Drawing it will mix with the Media.

I want to make it as much as easier to select a color. In windows forms it was easy just typing Color.Red.

The result in the end should be like this :

199800-rich2.jpg

but using the method in the ColorRichTextBoxText class

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,710 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,639 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
790 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 47,336 Reputation points Microsoft Vendor
    2022-05-09T03:15:32.567+00:00

    For modifying the color of TextRange, you could try the following methods.
    MainWindow.xaml:

     <StackPanel>  
            <RichTextBox x:Name="c_richTextBox1"  Height="120" Width="400" Background="AliceBlue" >  
                <FlowDocument>  
                    <Paragraph>  
                        <Run>Paragraph 111111111111111111</Run>  
                    </Paragraph>  
                    <Paragraph>  
                        <Run>Paragraph 222222222222222222</Run>  
                    </Paragraph>  
                    <Paragraph>  
                        <Run>Paragraph 33333333333333333333333</Run>  
                    </Paragraph>  
                </FlowDocument>  
            </RichTextBox>  
            <TextBox x:Name="Color" Text="{Binding MyColor,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"  Height="40" Width="200" Background="LightSeaGreen"/>  
            <Button Content="setForeground"  Width="175" Click="c_setForegroundButton_Click"/>  
            <Button Content="clearForeground" Width="175" Click="c_clearForegroundButton_Click"/>  
    
        </StackPanel>  
    

    MainWinodw.xaml.cs:
    200084-d.txt

    The result:
    199958-3.gif


    If the response is helpful, please click "Accept Answer" and upvote it.
     Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread. 

    [5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html