标签文字无法正确显示

时间:2017-04-07 06:30:43

标签: visual-studio visual-studio-2017

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 个答案:

答案 0 :(得分:0)

您的代码失败的原因是因为在您的子字符串中,您没有将长度限制为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