SelectionColor property not workingin GUI

Hi

I am trying to change the text color but received below error.

The property ‘SelectionColor’ cannot be found on this object. Verify that the property exists and can be set.
At C:\Users\Muhammad Suhail\Desktop\script\work\Patching Team\temp.ps1:72 char:9

  • $outputBox.SelectionColor = “#FF0000
  • CategoryInfo : InvalidOperation: (:slight_smile: [], RuntimeException
  • FullyQualifiedErrorId : PropertyAssignmentException

 

Code:

        try {
        $RebootTime = Get-WmiObject win32_operatingsystem -ComputerName $computer -ErrorAction Stop | ForEach-Object { $_.ConvertToDateTime($_.LastBootUpTime) }

        $outputBox.Appendtext("Reboot time: $RebootTime`n")

    } 

    catch {

        $outputBox.Font = "Calibri,11,style=Bold"

        $outputBox.SelectionColor = "#FF0000"

        $outputBox.Appendtext("Reboot time - Error`n")

    }

    finally {

        $Error.Clear()

    }</pre>

You haven’t provided enough information for this forum to help you.

Without knowing what class of object $outputBox is, no one will be able to tell you if SelectionColor is a valid property and if it can be set or is read only.

The error implies that the property does not exist. You can check this by piping $outputBox to Get-Member

$outputBox | Get-Member -MemberType -Property

 

Code Updated:

$outputBox = New-Object System.Windows.Forms.RichTextBox 
$outputBox.Multiline = $true 
$outputBox.BackColor = "#FDFEFE" 
$outputBox.Width = 960
$outputBox.Height = 630
$outputBox.ReadOnly = $true 
$outputBox.ScrollBars = "Both" 
$outputBox.WordWrap = $false 
$outputBox.location = new-object system.drawing.point(10, 310) 
$outputBox.Font = "Calibri,11,style=Bold" 
$Form.controls.Add($outputBox)

function healthcheckinfo {
$outputBox.Clear() 
$computers = $InputBox.lines.Split("`n")
foreach ($computer in $computers) {
$outputBox.AppendText($computer + ":-" + "`n")
try {
$RebootTime = Get-WmiObject win32_operatingsystem -ComputerName $computer -ErrorAction Stop | ForEach-Object { $_.ConvertToDateTime($_.LastBootUpTime) }
$outputBox.Appendtext("Reboot time: $RebootTime`n")
} 
catch {
$outputBox.Font = "Calibri,11,style=Bold"
$outputBox.SelectionColor = "#FF0000"
$outputBox.Appendtext("Reboot time - Error`n")
}
finally {
$Error.Clear()
}

That should work fine with RichTextBox. In fact, after adding the addtional code necessary to create the form, your code works as expected and I don’t get the error you posted in your original post.

As previously advised, I would pipe $outputBox to Get-Member to check it’s the class of object you think it is.