如何在 powershell 文本框中为特定字符串着色

如何在 powershell 文本框中为特定字符串着色

如何在 powershell 表单的文本框中为特定字符串着色。我不使用任何“工作室”应用程序。

输出例如:

Searching...
a - found
b - Not found
c - found

预期结果:

Searching...
a - found
b - \red\Not found\red\
c - found

我听说过RichTextBox,但是当使用下面的代码时,它不会打印任何内容,只是一个空字符串(代码中没有出现错误)

$StatisticsBox = New-Object System.Windows.Forms.RichTextBox 
    $StatisticsBox.Location = New-Object System.Drawing.Size(170,30) 
    $StatisticsBox.Size = New-Object System.Drawing.Size(150,320) 
    $StatisticsBox.MultiLine = $True 
    $StatisticsBox.ScrollBars = "Vertical" 
    $StatisticsBox.ReadOnly=$True
    $Form.Controls.Add($StatisticsBox) 

$StatisticsBox.SelectionColor = 'red' <---- also tried "color.red"
$StatisticsBox.text = "`r`nNot Found:`r`n" + $StatisticsBox.AppendText("colored stirng") + "`r`n" +$StatisticsBox.text

答案1

对我有用。

在此处输入图片描述

Add-Type -AssemblyName System.Windows.Forms

$Form = New-Object system.Windows.Forms.Form
$Form.Width = 300
$Form.Height = 200

$StatisticsBox = New-Object System.Windows.Forms.RichTextBox 
$StatisticsBox.Location = New-Object System.Drawing.Size(50,30) 
$StatisticsBox.Size = New-Object System.Drawing.Size(150,100) 
$StatisticsBox.MultiLine = $True 
$StatisticsBox.ScrollBars = "Vertical" 
$StatisticsBox.ReadOnly=$True
$Form.Controls.Add($StatisticsBox) 

$StatisticsBox.SelectionColor = 'red'
$StatisticsBox.text = "`r`nNot Found:`r`n" + $StatisticsBox.AppendText("colored stirng") + "`r`n" +$StatisticsBox.text

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

波士古来总是我的第一站。

相关内容