Powershell 创建具有不同颜色的多条线的最佳方法

Powershell 创建具有不同颜色的多条线的最佳方法,powershell,fonts,textbox,label,multiline,Powershell,Fonts,Textbox,Label,Multiline,我试图找出,什么是最好的方式,以输出不同颜色的多行。 我有几个选择,但每一个都在某种程度上具有挑战性。我认为我的可能性是: 列表框 文本框 标签 我正在建立一个词汇训练器,在测试过程中,你必须写单词。在测试结束时,我想总结一下测试 第一个挑战是,测试的长度不同,这意味着它只能测试10个单词或更多。目标是,向每个问题展示答案,并分析答案是否正确和错误,如果错误,错误在哪里。这部分我已经解决了 但现在的问题是,如何呈现结果 Listbox和Textbox当然更容易处理,但如果我理解正确的话,输出

我试图找出,什么是最好的方式,以输出不同颜色的多行。 我有几个选择,但每一个都在某种程度上具有挑战性。我认为我的可能性是:

  • 列表框
  • 文本框
  • 标签
我正在建立一个词汇训练器,在测试过程中,你必须写单词。在测试结束时,我想总结一下测试

第一个挑战是,测试的长度不同,这意味着它只能测试10个单词或更多。目标是,向每个问题展示答案,并分析答案是否正确和错误,如果错误,错误在哪里。这部分我已经解决了

但现在的问题是,如何呈现结果

Listbox和Textbox当然更容易处理,但如果我理解正确的话,输出具有不同的颜色将是一个挑战

标签将更容易处理,但我正在与多行进行斗争,到目前为止,我唯一的解决方案是为每个结果生成一个不同的标签,这意味着,我必须根据输出的总和构建标签,这意味着10个测试问题将需要10个标签

当您运行下面的测试脚本时,您将看到结果,您写道:错误以红色字母显示

这样的想法是,每一行都有这样的输出,错误显示在哪里,如果正确的话,它有一个绿色,显示出来,一切正常

Clear-Host

$Frage = 'Hello World'
$Antwort = 'Hello Warld'
$Script:Counter = 0
$wordlength = $Frage.Length

Do {
    if ($Antwort[$Script:Counter] -ne $Frage[$Script:Counter]) { break }
    Write-Host $Frage[$Script:Counter] -NoNewline;
    Write-Host -ForegroundColor Red '    '$Antwort[$Script:Counter]
    $Script:Counter++

} # End of 'Do'
Until ($Script:Counter -eq $wordlength)

If ($Antwort.Length -gt $Frage.Length) {

    $TooShortDifference = $Antwort[($Frage.Length)..($Antwort.Length)]
    Write-Host ''
    Write-Host -ForegroundColor Red 'Your Answer has too many letters'
    Write-Host '' 
    Write-Host -ForegroundColor Green -NoNewline 'You wrote:     ' 
    Write-Host $Firstpart -NoNewline;
    Write-Host $TooShortDifference -ForegroundColor Red 
    Write-Host ''
    Write-Host -ForegroundColor Green -NoNewline 'The answer is: '
    Write-Host -ForegroundColor White $Frage
    Write-Host ''
}

Elseif ($Antwort.Length -lt $Frage.Length) {

    $TooLongDifference = $Frage[($Antwort.Length)..$Frage.Length]
    Write-Host ''
    Write-Host -ForegroundColor Red 'Your Answer has not enough letters'
    Write-Host '' 
    Write-Host -ForegroundColor Green -NoNewline 'You wrote:     ' 
    Write-Host -ForegroundColor White $Antwort
    Write-Host ''
    Write-Host -ForegroundColor Green -NoNewline 'The answer is: '
    Write-Host -ForegroundColor White $Antwort -NoNewline
    Write-Host -ForegroundColor Red $TooLongDifference -Separator ''
}

Elseif ($Antwort.Length -eq $Frage.Length) {

    $SameLengthWithError = $Frage[0..($Script:Counter - 1)]
    Write-Host ''
    Write-Host -ForegroundColor Red 'Your Answer has a letter mismatch'
    Write-Host ''
    Write-Host -ForegroundColor White 'You Wrote:     ' -NoNewline
    Write-Host $SameLengthWithError -NoNewline -Separator ''
    Write-Host $Antwort[$Script:Counter] -ForegroundColor Red  -NoNewline
    Write-Host $Antwort[($Script:Counter + 1)..$Antwort.Length] -Separator ''
    Write-Host ''
    Write-Host -ForegroundColor Green 'The Answer is: ' -NoNewline
    Write-Host -ForegroundColor Green $Frage

}

Else {

    Write-Host -ForegroundColor Green 'Everything was correct'
}

如果要构建类似的内容,您会使用哪种方式以及如何显示多行,字体中每行的颜色会在哪里发生变化?

请查看。我想你应该用RichTextboxHello Theo,这样行得通。我需要研究和理解这一点,但它看起来很有希望,比现在好多了