0

我试图弄清楚,输出不同颜色的多行的最佳方法是什么。我有几种选择,但每一种都在某种程度上具有挑战性。我认为我拥有的可能性是:

  • 列表框
  • 文本框
  • 标签

我正在建立一个词汇训练器,在测试期间,你必须写单词。在测试结束时,我想总结一下测试。

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

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

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

标签会更容易处理,但我在 Multiline 上苦苦挣扎,到目前为止,我唯一的解决方案是为每个结果生成一个不同的标签,这意味着,我必须根据输出的总和构建标签,这意味着 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'
}

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

4

0 回答 0