Powershell gui textbox with multiple color lines

邮差的信 提交于 2021-02-17 05:50:28

问题


Im having trouble with powershell textbox here is the definition of it:

$ResultsTextBox = New-Object System.Windows.Forms.TextBox
$ResultsTextBox.Location = New-Object System.Drawing.Size(780,40)
$ResultsTextBox.Size = New-Object System.Drawing.Size(450,340)

i would like to create a function which output text to this text box but i would like to generate the text either red or green depending of my choice.. but when i do this:

function LinkFn {
$ResultsTextBox.clear()
$SWITCH = Get-ADOrganizationalUnit -filter *  -Property CanonicalName | Where-Object {$_.CanonicalName -eq $listBox2.SelectedItem}
    forEach ($line in $listBox1.selecteditems){
         try {
              $ResultsTextBox.ForeColor ='green' 

             New-GPlink -name $line -target $SWITCH -ErrorAction STOP | Out-null
             $ResultsTextBox.AppendText("`n GPO: $line HAVE BEEN LINKED Successfully.`n") 
            }
            catch{
            $ResultsTextBox.ForeColor ='red'
            $ResultsTextBox.AppendText("`n COULDN NOT LINK GPO: $line `n")
    }
 }

it changes all the lines, when the result i wanted could be 1 line in red 2 lines in green for example


回答1:


You need to use a RichTextBox control for this instead of a regular TextBox.

For demo here's a small form that fills lines in different colors to a RichTextBox:

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

# helper function to write text in a given color 
# to the specified RichTextBox control
function Append-ColoredLine {
    param( 
        [Parameter(Mandatory = $true, Position = 0)]
        [System.Windows.Forms.RichTextBox]$box,
        [Parameter(Mandatory = $true, Position = 1)]
        [System.Drawing.Color]$color,
        [Parameter(Mandatory = $true, Position = 2)]
        [string]$text
    )
    $box.SelectionStart = $box.TextLength
    $box.SelectionLength = 0
    $box.SelectionColor = $color
    $box.AppendText($text)
    $box.AppendText([Environment]::NewLine)
}

$form = New-Object System.Windows.Forms.Form
$form.Width = 400
$form.Height = 500

$richText = New-Object System.Windows.Forms.RichTextBox
$richText.Location = [System.Drawing.Point]::new(10,10)
$richText.Size = [System.Drawing.Size]::new(364,350)
$richText.Font = [System.Drawing.Font]::new('Calibri', 14)
$richText.Anchor = 'Top','Right','Bottom','Left'
$form.Controls.Add($richText)

$button = New-Object System.Windows.Forms.Button
$button.Location = [System.Drawing.Point]::new(10,400)
$button.Size = [System.Drawing.Size]::new(80,30)
$button.Text = 'Test'
$button.Anchor = 'Bottom','Left'
$button.Add_Click({
    $richText.Clear()
    # write green lines
    Append-ColoredLine $richText Green "GPO: 'gpo_A' has been linked Successfully"
    Append-ColoredLine $richText Green "GPO: 'gpo_B' has been linked Successfully"
    # write red line
    Append-ColoredLine $richText Red "Could not link GPO: 'gpo_C'"

    # insert blank line
    $richText.AppendText([Environment]::NewLine)

    # write various lines in different colors
    'Blue','DarkGoldenrod','DarkCyan','OliveDrab','Chocolate','Crimson' | ForEach-Object {
        Append-ColoredLine $richText $_ "Some text using color '$_'" 
    }
})
$form.Controls.Add($button)

[void] $form.ShowDialog()
$form.Dispose()

When pressing the Test button on this form, colored lines are written

Hope that helps



来源:https://stackoverflow.com/questions/61817387/powershell-gui-textbox-with-multiple-color-lines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!