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

时间:2019-09-13 05:04:10

标签: powershell fonts textbox label multiline

我正在尝试找出输出具有不同颜色的多行的最佳方法是什么。 我有几种选择,但每种选择都在某种程度上具有挑战性。我想我拥有的可能性是:

  • 列表框
  • 文本框
  • 标签

我正在建立一个词汇训练师,在测试过程中,您必须写单词。在测试结束时,我想总结一下测试。

第一个挑战是,测试的长度不同,这意味着它只能是10个单词或更多的测试。目标是向每个问题显示答案,并分析其正确与错误以及错误的地方。我已经解决了那部分。

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

列表框和文本框肯定更易于处理,但如果我理解正确的话,输出具有不同的颜色将是一个挑战。

标签会更容易处理,但是到目前为止,我在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'
}

如果您要构建类似的东西,将使用哪种方式以及如何显示多行,每行的字体颜色会在其中更改?

0 个答案:

没有答案