Home > Mobile >  wpf c# function for text decoration
wpf c# function for text decoration

Time:05-19

I am in the process of converting a Powershell script to a WPF C# script. I am trying to convert a powershell function that adds text to the richtextbox and assigns a color. I currently have multiple uses of the same code, which I know is inefficient. What I want is a function/method that I can reuse whenever I am assigning new text and colors. Here is what I have:

rich text box looks like this on WPF xml

<RichTextBox x:Name="richBox"
             Grid.Row="3"
             Grid.ColumnSpan="3"
             Margin="10,10,10,10"
             HorizontalAlignment="Left">
</RichTextBox>

Here is my C# code

TextRange rtbRange = new TextRange(richBox.Document.ContentEnd, richBox.Document.ContentEnd);
rtbRange.Text = "Add some text here";
rtbRange.ApplyPropertyValue(TextElement.ForegroundProperty, System.Windows.Media.Brushes.DodgerBlue);

//some code is here

TextRange rtbRange2 = new TextRange(richBox.Document.ContentEnd, richBox.Document.ContentEnd);
rtbRange2.Text = "add some more text later with the same property value";
rtbRange2.ApplyPropertyValue(TextElement.ForegroundProperty, System.Windows.Media.Brushes.DodgerBlue);

//some more code is here

TextRange rtbRange3 = new TextRange(richBox.Document.ContentEnd, richBox.Document.ContentEnd);
rtbRange3.Text = "add some more text later with a different color";
rtbRange3.ApplyPropertyValue(TextElement.ForegroundProperty, System.Windows.Media.Brushes.DarkGreen);

Similar powershell function:

function add2RTB{
 Param([string]$rtbText, 
       [string]$rtbColor = "Black")
   $RichTextRange = New-Object System.Windows.Documents.TextRange( 
   $syncHash.richBox.Document.ContentEnd,$syncHash.richBox.Document.ContentEnd ) 
   $RichTextRange.Text = $rtbText 
   $RichTextRange.ApplyPropertyValue(([System.Windows.Documents.TextElement]::ForegroundProperty ), $rtbColor) 
}
#Usage: 
add2RTB -rtbText "adding some text" -rtbColor "DodgerBlue"
#some code here
add2RTB -rtbText "adding more text" -rtbColor "DarkGreen"

Is there a method or function in C# for use with WPF that is similar to the powershell function? I am still fairly new to C#, and converting the sometimes complicated scripts that I know from powershell is a steep learning curve.

CodePudding user response:

Just translate the PS function 'add2RTB' to C#.
It's just the same as in Powershell, two parameters, and a TextRange to create...

using System.Windows.Media;
...

void Add2RTB (string text, Brush brush) {
   var rtbRange = new TextRange(richBox.Document.ContentEnd, richBox.Document.ContentEnd) { Text = text };
   rtbRange.ApplyPropertyValue(TextElement.ForegroundProperty, brush);
}

// usage:    

Add2RTB ("Add some text here", Brushes.DodgerBlue);
//some code is here
Add2RTB ("add some more text later with the same property value", Brushes.DodgerBlue);
//some more code is here
Add2RTB ("add some more text later with a different color", Brushes.DarkGreen);
  • Related