Search code examples
c#wpfxamlrichtextbox

using C# Format colour of line in RichTextBox based on the content of the string


I am using C# for the first time and my goal is to create an application that outputs text to a window. The windows structure is defined in a .XAML file and the text in question is to be added to a RichTextBox. Depending on the content of the string to be appended I want the text color to be different. I.E if the string contains the word "passed" I want the text to be green and red if it contains "failed". I have found a method that almost works but instead changes the color of all the text in the box instead of just the desired line.

The name of the RichTextBox is "TextResults" as shown below: enter image description here

I have used the ForeGround property and assigned it to a SolidColorBrush object

                    if (dispText[1].Contains("passed")) textResults.Foreground = new SolidColorBrush(Colors.Green);
                    else if (dispText[1].Contains("failed")) textResults.Foreground = new SolidColorBrush(Colors.Red);
                    else textResults.Foreground = new SolidColorBrush(Colors.Black);

                    textScript.AppendText(dispText[0] + Environment.NewLine);
                    textResults.AppendText(dispText[1] + Environment.NewLine);

The problem is, this method applies the color to all strings in the RichTextBox, so assuming the final string contains "failed", all the text goes red. Output

I have looked online at lots of different methods and none seem to help, this is my first stack overflow post so apologies if I have committed any sins in this post. Any help would be much appreciated.


Solution

  • Use this simple Example, with MainWindow.xaml.cs:

    using System.Windows.Documents;
    using System.Windows.Media;
    namespace Stack2
    {
        public partial class MainWindow
        {
            public MainWindow()
            {
                InitializeComponent();
                AppendTextWithColor("Text1 Req passed",Brushes.Blue);
                AppendText("New status eq");
                AppendText("Text1 Req failed");
                AppendText("Text1 Req passed");
                AppendText("New status eq");
                AppendText("New status failed");
            }
    
            private void AppendText(string text)
            {
                Paragraph par = new Paragraph(new Run(text));
                
                if (text.Contains("passed"))
                {
                    par.Foreground = Brushes.Blue;
                    
                } else if (text.Contains("failed"))
                {
                    par.Foreground = Brushes.Red;
    
                }
                else
                {
                    par.Foreground = Brushes.Black;
                }
                textResults.Document.Blocks.Add(par);
            }
            private void AppendTextWithColor(string text, Brush c)
            {
                Paragraph par = new Paragraph(new Run(text));
                par.Foreground = c;
                textResults.Document.Blocks.Add(par);
            } 
            
        }
    }
    

    And MainWindow.xaml:

    <Window x:Class="Stack2.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:Stack2"
            mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <StackPanel>
                <RichTextBox x:Name="textResults">
                    <FlowDocument>
                        <Paragraph>
                            This is flow content and you can <Bold>edit me!</Bold>
                        </Paragraph>
                    </FlowDocument>
                </RichTextBox>
            </StackPanel>
        </Grid>
    </Window>