具有多条颜色线的 Powershell gui 文本框 [英] Powershell gui textbox with multiple color lines
问题描述
我在使用 powershell 文本框时遇到问题这是它的定义:
$ResultsTextBox = 新对象 System.Windows.Forms.TextBox$ResultsTextBox.Location = 新对象 System.Drawing.Size(780,40)$ResultsTextBox.Size = 新对象 System.Drawing.Size(450,340)
我想创建一个将文本输出到此文本框的函数,但我想生成红色或绿色文本取决于我的选择..但是当我这样做时:
function LinkFn {$ResultsTextBox.clear()$SWITCH = Get-ADOrganizationalUnit -filter * -Property CanonicalName |Where-Object {$_.CanonicalName -eq $listBox2.SelectedItem}forEach($listBox1.selecteditems 中的 $line){尝试 {$ResultsTextBox.ForeColor ='绿色'New-GPlink -name $line -target $SWITCH -ErrorAction STOP |外空$ResultsTextBox.AppendText("`n GPO: $line 已成功链接.`n")}抓住{$ResultsTextBox.ForeColor ='红色'$ResultsTextBox.AppendText("`n 不能链接 GPO:$line `n")}}
它改变了所有的行,例如当我想要的结果可能是红色的 1 行 绿色的 2 行
您需要使用
希望有帮助
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
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
这篇关于具有多条颜色线的 Powershell gui 文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!