Adding new lines to RichTextbox with different colors

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
Jzzocco86
Posts: 8
Last visit: Tue Jan 30, 2024 12:34 pm

Adding new lines to RichTextbox with different colors

Post by Jzzocco86 »

I am trying to add lines to a richtextbox using .appendtext. Things work fine with the first line, however once I add an new line the whole box changes to the new color.

Below is the code that runs on a submit button. The colors are choosen from a set of radio buttons ($global:textcolor) and there is a text box to type in some text ($mainTextbox1)


$mainButtonSubmit_Click = {
$mainRichTextbox1.ForeColor = [Drawing.Color]::$($global:textcolor)
$mainRichTextbox1.AppendText("$($mainTextbox1.text)`n")
}

Any assistance would be appreciated
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Adding new lines to RichTextbox with different colors

Post by jvierra »

You have to select the text to add the color or use RTF color commands with the test.
User avatar
Jzzocco86
Posts: 8
Last visit: Tue Jan 30, 2024 12:34 pm

Re: Adding new lines to RichTextbox with different colors

Post by Jzzocco86 »

Thanks for the advice. I am new to using powershell GUIs can you provide a code snippet or article to help.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Adding new lines to RichTextbox with different colors

Post by jvierra »

Here is an old demo of how to add color.
Attachments
Demo-RTFColors.psf
(18.94 KiB) Downloaded 1691 times
User avatar
Jzzocco86
Posts: 8
Last visit: Tue Jan 30, 2024 12:34 pm

Re: Adding new lines to RichTextbox with different colors

Post by Jzzocco86 »

Thanks for the response. I tried that last night but it appears that .SelectionColor is no longer a command. i get the below error message. Am I doing something wrong?


The property 'SelectionColor' cannot be found on this object. Verify that the property exists and can be set.
MainForm.psf (44, 2): ERROR: At Line: 44 char: 2
ERROR: + $mainRichTextbox1.SelectionColor='red'
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : InvalidOperation: (:) [], RuntimeException
ERROR: + FullyQualifiedErrorId : PropertyAssignmentException
ERROR:

Heres the updated snippet.

$mainButtonSubmit_Click = {
#$mainRichTextbox1.ForeColor = [Drawing.Color]::$($global:textcolor)
$mainRichTextbox1.SelectionColor='red'
$mainRichTextbox1.AppendText("$($mainTextbox1.text)`n")
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Adding new lines to RichTextbox with different colors

Post by jvierra »

Did you actually try and run my example. It works so your form must not be using a richtextbox.

"SelectionColor" is not a command. It is a property.
User avatar
Jzzocco86
Posts: 8
Last visit: Tue Jan 30, 2024 12:34 pm

Re: Adding new lines to RichTextbox with different colors

Post by Jzzocco86 »

My apologies for using the incorrect terminology, I am still learning.

Thank you I was not apparently using a richtextbox, I was using a normal textbox.

Once I changed it to a richtextbox things started working as expected.

Thanks again for the help
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Adding new lines to RichTextbox with different colors

Post by jvierra »

No need to apologize. It is a common mistake but know you know.
This topic is 7 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked