1

これで苦労して、うまくいかないようです。テキストボックスにテキストがあります。次に、コンボ ドロップ ボックスを使用して、テキスト フィールドに表示される文字を選択しました。次に、選択した文字がテキスト ボックスに表示される回数を表示する必要があります。以下にリストされているのは、いくつかのエラーが発生している私のコードです。私が得ている大きなエラーは

「インデックスと長さは、文字列内の場所を参照する必要があります。パラメータ名: 長さ」

サブストリング機能に関係していると思います。テキストボックスの文字の長さで何かをしなければならないと思います。これを正しく機能させるための助けは大歓迎です。

 Private Sub cboSelectText_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cboSelectText.SelectedIndexChanged


        'value to find
        Dim strLetterToFind As String

        'String to search
        Dim strStringToSearch As String = txtWordsToScan.Text

        'Current Character
        Dim chrCurrentCharacter As Char

        'Length of text
        Dim intLengthOfText As Integer
        intLengthOfText = strStringToSearch.Length

        'Letter totals
        'Dim intLoopCounter As Integer

        'Count for the display
        Dim intLetterA, intLetterE, intLetterI, intLetterO, intLetterU, intLetterCH, intWords As Integer


        Select Case cboSelectText.SelectedIndex
            Case 1
                strLetterToFind = "A"
            Case 2
                strLetterToFind = "E"
            Case 3
                strLetterToFind = "I"
            Case 4
                strLetterToFind = "O"
            Case 5
                strLetterToFind = "U"
            Case 6
                strLetterToFind = "CH"
            Case 7
                strLetterToFind = " "
            Case Else
                strLetterToFind = String.Empty

        End Select

        For intLoopCounter As Integer = 0 To intLengthOfText
            If chrCurrentCharacter = strStringToSearch.Substring(intLoopCounter, 1).ToUpper Then
                If strLetterToFind = "A" Then
                    intLetterA += 1
                    lblANumberTotal.Text = CStr(intLetterA)
                ElseIf chrCurrentCharacter = "E" Then
                    intLetterE += 1
                    lblENumberTotal.Text = CStr(intLetterE)
                ElseIf chrCurrentCharacter = "I" Then
                    intLetterI += 1
                    lblINumberTotal.Text = CStr(intLetterI)
                ElseIf chrCurrentCharacter = "O" Then
                    intLetterO += 1
                    lblONumberTotal.Text = CStr(intLetterO)
                ElseIf chrCurrentCharacter = "U" Then
                    intLetterU += 1
                    lblUNumberTotal.Text = CStr(intLetterU)
                ElseIf chrCurrentCharacter = "CH" Then
                    intLetterCH += 1
                    lblCHNumberTotal.Text = CStr(intLetterCH)
                ElseIf chrCurrentCharacter = " " Then
                    intWords += 1
                    lblTotalNumberWords.Text = CStr(intWords)
                End If

            End If

        Next

    End Sub
4

1 に答える 1

1

これはあなたが求めていることをするはずです:

  Dim strLetterToFind  As String

        strLetterToFind = ComboBox1.SelectedItem


        Try
            lblTotalNumberWords.Text =  Regex.Matches(TextBox1.Text,Regex.Escape(strlettertofind)).Count.ToString)
        Catch ex As Exception
            lblTotalNumberWords.Text = "Please select a option in the combo box"
        End Try

System.Text.RegularExpressions を機能させるには、インポートする必要があります。

これに問題がある場合はお知らせください

于 2012-11-22T05:23:21.723 に答える