title: PowerShell richtextbox change colour of new line
body: <p>Nice and simple question, but a minefield of c# responses on Google that make sense to a PowerShell person like me (I need to learn!)</p>
  
  <p>I have a richtextbox created using XAML, and I'd like to have the ability to add a line in a different coloured font.</p>
  
  <p>Most of the Saipen results suggest $formLogReport.SelectionColor but no such property exists.</p>
  
  <p>I've actually found something that does work but it's overkill and goes beyond what I know about PowerShell - I'm reluctant to use code I don't understand.</p>
  
  <p><a href="http://vcloud-lab.com/entries/powercli/powershell-gui-format-text-on-textbox-and-richtextbox" rel="nofollow noreferrer">http://vcloud-lab.com/entries/powercli/powershell-gui-format-text-on-textbox-and-richtextbox</a></p>
  
  <p>For reference, the code below uses the function from the link provided.</p>
  
  <pre><code>[void][System.Reflection.Assembly]::LoadWithPartialName( 'presentationframework' )
  [void][System.Reflection.Assembly]::LoadWithPartialName( 'System.Windows.Forms' )
  
  
  [xml]$xaml =  @'
  &lt;Window
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      Title="RichTextBox Example" Height="530" Width="740" &gt;
  
  &lt;Grid Name="GridName"&gt;
  
      &lt;Label Name="SetupLabel" Content="Setup type" FontSize="11" HorizontalAlignment="Left" VerticalAlignment="Top"
          Margin="30,25,30,30" Height="25" Width="320" /&gt;
      &lt;ComboBox Name="SetupList" HorizontalAlignment="Left" VerticalAlignment="Top"
          Margin="30,55,30,30" Height="25" Width="320" /&gt;
  
      &lt;Label Name="SubsiteLabel" Content="Text in here will be a different size" FontSize="11" 
          HorizontalAlignment="Left" VerticalAlignment="Top" Margin="30,265,30,30" Height="25" Width="320" /&gt;
      &lt;TextBox Name="SubsiteBox" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="30,295,0,0" 
          Height="25" Width="320" TextWrapping="Wrap" TextAlignment="Left" VerticalContentAlignment="Center" /&gt;
  
      &lt;Label Name="StuffLabelLabel" Content="Enter Stuff to show up" FontSize="11" 
          HorizontalAlignment="Left" VerticalAlignment="Top" Margin="30,345,30,30" Height="25" Width="320" /&gt;
      &lt;TextBox Name="StuffBox" FontSize="11" HorizontalAlignment="Left" VerticalAlignment="Top" 
          Margin="30,375,0,0" Height="25" Width="320" TextWrapping="Wrap" TextAlignment="Left" 
          VerticalContentAlignment="Center" /&gt;
  
      &lt;Label Name="LogLabel" Content="Log..." HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="11"
          Margin="390,25,0,0" Height="25" Width="320"  /&gt;
  
      &lt;RichTextBox Name="LogReport" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="390,55,0,0" 
          Height="345" Width="300"  &gt;
          &lt;FlowDocument&gt;
              &lt;Paragraph&gt;"HI THERE"
                  &lt;Run Text=""/&gt;
              &lt;/Paragraph&gt;  
          &lt;/FlowDocument&gt;
      &lt;/RichTextBox&gt;
  
      &lt;Button Name="GoButton" Content="Go!" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="625,435,0,0"
          Height="25" Width="65" IsEnabled="False" /&gt;
  
  
  &lt;/Grid&gt;
  &lt;/Window&gt;
  '@
  
  
  $reader = ( New-Object System.Xml.XmlNodeReader $xaml )
  
  try {
  $Form = [Windows.Markup.XamlReader]::Load( $reader )
  
  }
  catch {
  Write-Warning "Unable to parse XML, with error: $( $Error[0] )`n "
  }
  
  
  #===========================================================================
  # Load XAML Objects / Form Changes &amp; Conditions
  #===========================================================================
  
  
  $xaml.SelectNodes( "//*[@Name]") | ForEach-Object { Set-Variable -Name "form$( $_.Name )" -Value $Form.FindName( $_.Name ) }
  
  
  
  function Format-RichTextBox {  
  #https://msdn.microsoft.com/en-us/library/system.windows.documents.textelement(v=vs.110).aspx#Propertiesshut  
  param (  
    [parameter(Position=0, Mandatory=$true, ValueFromPipelineByPropertyName=$true)]  
    [System.Windows.Controls.RichTextBox]$RichTextBoxControl,  
    [String]$Text,  
    [String]$ForeGroundColor = 'Black',  
    [String]$BackGroundColor = 'White',  
    [String]$FontSize = '12',  
    [String]$FontStyle = 'Normal',  
    [String]$FontWeight = 'Normal',  
    [Switch]$NewLine  
  )
  
  $ParamOptions = $PSBoundParameters  
  $RichTextRange = New-Object System.Windows.Documents.TextRange( $RichTextBoxControl.Document.ContentEnd, $RichTextBoxControl.Document.ContentEnd )  
  if ($ParamOptions.ContainsKey('NewLine')) {  
    $RichTextRange.Text = "`n$Text"  
  }  
  else {  
    $RichTextRange.Text = $Text  
  }  
  
  $Defaults = @{ForeGroundColor='Black';BackGroundColor='White';FontSize='12'; FontStyle='Normal'; FontWeight='Normal'}  
  foreach ($Key in $Defaults.Keys) {  
    if ($ParamOptions.Keys -notcontains $Key) {  
      $ParamOptions.Add($Key, $Defaults[$Key])  
    }  
  }   
  
  $AllParameters = $ParamOptions.Keys | Where-Object {@('RichTextBoxControl','Text','NewLine') -notcontains $_}  
  foreach ($SelectedParam in $AllParameters) {  
    if ($SelectedParam -eq 'ForeGroundColor') {$TextElement = [System.Windows.Documents.TextElement]::ForegroundProperty}  
    elseif ($SelectedParam -eq 'BackGroundColor') {$TextElement = [System.Windows.Documents.TextElement]::BackgroundProperty}  
    elseif ($SelectedParam -eq 'FontSize') {$TextElement = [System.Windows.Documents.TextElement]::FontSizeProperty}  
    elseif ($SelectedParam -eq 'FontStyle') {$TextElement = [System.Windows.Documents.TextElement]::FontStyleProperty}  
    elseif ($SelectedParam -eq 'FontWeight') {$TextElement = [System.Windows.Documents.TextElement]::FontWeightProperty}  
    $RichTextRange.ApplyPropertyValue($TextElement, $ParamOptions[$SelectedParam])  
  }  
    }
  
  
  
  
  $formstuffbox.Add_KeyDown( {
  If ( $args[1].key -eq 'Return' ) {
      $formLogReport.AppendText( "$( $formstuffbox.text )`n" )
  }
  } ) 
  
  
  $formsubsitebox.Add_KeyDown( {
  If ( $args[1].key -eq 'Return' ) {
      Format-RichTextBox -RichTextBoxControl $formLogReport -Text $formsubsitebox.text -ForeGroundColor Red
  }
  } ) 
  
  
  $form.ShowDialog()
  </code></pre>
  
  <p>Does anyone know of a simpler method? It's only ever going to be used for errors, so only ever needs to become red.</p>