![如何在 powershell 文本框中为特定字符串着色](https://linux22.com/image/1546605/%E5%A6%82%E4%BD%95%E5%9C%A8%20powershell%20%E6%96%87%E6%9C%AC%E6%A1%86%E4%B8%AD%E4%B8%BA%E7%89%B9%E5%AE%9A%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9D%80%E8%89%B2.png)
如何在 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()
波士古来总是我的第一站。