标签文本显示不正确

Label text not displaying correctly

Private Sub btnFind_Click(sender As Object, e As EventArgs) Handles btnFind.Click
    lblNumber.Text = ""
    strLttrInput = InputBox("Please enter a letter or phrase:", "Find the corrresponding number to the letter(s)")
    With strLttrInput
        .ToLower()
        .Trim()
    End With
    intPhraseIndx = strLttrInput.Length - 1
    Dim strWord(intPhraseIndx) As String
    Dim strOutput As String = String.Empty

    For intCount = 0 To intPhraseIndx
        strWord(intCount) = strLttrInput.Substring(intCount)
    Next

    For intCount = 0 To intPhraseIndx
        Select Case strWord(intCount)
            Case "a", "b", "c"
                lblNumber.Text = lblNumber.Text & "2"
            Case "d", "e", "f"
                lblNumber.Text = lblNumber.Text & "3"
            Case "g", "h", "i"
                lblNumber.Text = lblNumber.Text & "4"
            Case "j", "k", "l"
                lblNumber.Text = lblNumber.Text & "5"
            Case "m", "n", "o"
                lblNumber.Text = lblNumber.Text & "6"
            Case "p", "r", "s"
                lblNumber.Text = lblNumber.Text & "7"
            Case "t", "u", "v"
                lblNumber.Text = lblNumber.Text & "8"
            Case "w", "x", "y"
                lblNumber.Text = lblNumber.Text & "9"
        End Select

        If strWord(intCount) = "q" Then
            lblNumber.Text = lblNumber.Text & "q"
        End If
        If strWord(intCount) = "z" Then
            lblNumber.Text = lblNumber.Text & "z"
        End If
    Next
End Sub

例如,当我输入"adgj"时,标签的文本应该是“2345”,但是当我输入运行时,标签的文本是“2”

您的代码失败的原因是因为在您的子字符串上,您没有将长度限制为 1。这意味着 strWord 的内容将是:

strWord(0) = adgj
strWord(1) = dgj
strWord(2) = gj
strWord(3) = j

您可以通过指定子字符串的长度来解决此问题:

strWord(intCount) = strLttrInput.Substring(intCount, 1)

作为旁注,不需要循环两次并存储字母,你可以这样做:

For intCount = 0 To intPhraseIndx
    Select Case strLttrInput.Substring(intCount, 1)
        ...
    End Select
Next