Changing text color in RichText within a function

I created a GUI with a richtext output box. I want to output text to the output box in different colors, so I used the SelectionColor property to change the color. I also wanted to test to see if it could be changed within a function. When I tried, I noticed that color changes for all of the text when I click on the Start button. I ran the debugger in PowershellISE and found out that the colors ARE changing, but after the function named TEST ends, that’s when the very first color that is set in the function sets all of the text to that color. Does anyone know why? Changing colors outside of function works correctly.

function Load-Form {
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$form = New-Object System.Windows.Forms.Form 
$form.Text = "Data Entry Form"
$form.Size = New-Object System.Drawing.Size(400,300) 
$form.StartPosition = "CenterScreen"

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)

$StartButton = New-Object System.Windows.Forms.Button
$StartButton.Location = New-Object System.Drawing.Point(75,90)
$StartButton.Size = New-Object System.Drawing.Size(75,23)
$StartButton.Text = "Start"
$StartButton.DialogResult = [System.Windows.Forms.DialogResult]::None
$StartButton.Add_Click({
Test
$OUTPUT_RICHTEXT.Text+="Get out"
})
$form.Controls.Add($StartButton)

$SCRIPT:OUTPUT_RICHTEXT=New-Object System.Windows.Forms.RichTextBox
$OUTPUT_RICHTEXT.Location=New-Object System.Drawing.Size(10,150)
$OUTPUT_RICHTEXT.Size=New-Object System.Drawing.Size(300,100)
$OUTPUT_RICHTEXT.Multiline=$True
$OUTPUT_RICHTEXT.ReadOnly = $True
$OUTPUT_RICHTEXT.BackColor = [Drawing.Color]::White
$OUTPUT_RICHTEXT.ScrollBars = "Vertical"
$form.Controls.Add($OUTPUT_RICHTEXT)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20) 
$label.Size = New-Object System.Drawing.Size(280,20) 
$label.Text = "Please enter the information in the space below:"
$form.Controls.Add($label) 

$textBox = New-Object System.Windows.Forms.TextBox 
$textBox.Location = New-Object System.Drawing.Point(10,40) 
$textBox.Size = New-Object System.Drawing.Size(260,20) 
$form.Controls.Add($textBox) 

$form.Topmost = $True

$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    $x = $textBox.Text
    $x
}
}

function Test {
$script:OUTPUT_RICHTEXT.SelectionColor = 'Magenta'
$script:OUTPUT_RICHTEXT.AppendText("This text will be Magenta. `n")
$script:OUTPUT_RICHTEXT.SelectionColor = 'Green'
$script:OUTPUT_RICHTEXT.AppendText("This text will be Green. `n")
$script:OUTPUT_RICHTEXT.SelectionColor = 'Blue'
$script:OUTPUT_RICHTEXT.AppendText("This text will be Blue. `n")
}

Load-Form

This is not a PoSH specific thing, but how you are coding it.

1 - If you are trying to change color as the text is written. You are not making any text selection to try to have an impact.
2 - Consider using the ForeColor method.
3 - Even using the right method, last in will win as the final result.

    function Test 
    {
        $script:OUTPUT_RICHTEXT.ForeColor = 'Magenta'
        $script:OUTPUT_RICHTEXT.AppendText("This text will be Magenta. `n")
        Start-Sleep -Seconds 1 
        $script:OUTPUT_RICHTEXT.ForeColor = 'Green'
        $script:OUTPUT_RICHTEXT.AppendText("This text will be Green. `n")
        Start-Sleep -Seconds 1 
        $script:OUTPUT_RICHTEXT.ForeColor = 'Blue'
        $script:OUTPUT_RICHTEXT.AppendText("This text will be Blue. `n")
    }

The way you are doing this, and my suggestion, color impact is for the whole form element (RichTextBox), not the individual lines in it.

So, your code would have to append, then select what you appended then try and color it, the that whole new line, then repeat.

See also this post, showing another trying to get creative using a RichTextBox.

Fomatting text in RichTextBox 'powershell.org/forums/topic/fomatting-text-in-richtextbox'

So in example, I do want the different colors for each line as specified. If it’s true when you say “Even using the right method, last in will win as the final result”, then why did all the text change to the first color of ‘Magenta’? Since Blue was the last color set after the function ends, shouldn’t all of the text be Blue?

Edit: I just saw my bug. In this part, I see I used .Text+= instead of .AppendText():

$StartButton.Add_Click({
Test
$OUTPUT_RICHTEXT.Text+="Get out"
})

Once I changed it to this:

$StartButton.Add_Click({
Test
$OUTPUT_RICHTEXT.AppendText("Get out")
})

Now it’s working.