0

我为我的一门课制作了这个循环,但由于某种原因,循环没有停止。这是我的代码

    Dim strScore As String
    Dim intNumTests As Integer 'counter
    Dim sngSumTests As Single 'accumulator
    Dim sngAverage As Single

    strScore = InputBox("Enter the score. Click cancel when finished.", "Score Entry")

    Do While strScore <> " "
        intNumTests = intNumTests + 1     ' update Counter
        sngSumTests = sngSumTests + Val(strScore)
        strScore = InputBox("Enter the score. Click cancel when finished.", "Score Entry")
    Loop
4

1 回答 1

2

要停止循环,您必须在 InputBox 中输入一个空格,而不是实际的单词space,而是按一次空格键。

代替

Do While strScore <> " "

你真正想要的是

Do While strScore <> "" AndAlso strScore <> " "

或者

Do While strScore.Length <> 0 AndAlso strScore <> " "

现在Cancel按钮将正常工作,如果您不键入任何内容并按下OK按钮,它将继续运行。

如果您按下OKInputBox 中的按钮并且输入为空,它将返回" "(空格)。
如果您按下CancelInputBox 中的按钮,它将返回""(空字符串)

编辑:原来OK在 .NET Framework 4 中按下一个空的 InputBox 也会返回(空字符串),所以这是不可能的?检测Cancel按钮。

于 2013-10-09T00:47:32.550 回答