2012-11-22 27 views
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 

回答

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爲它工作。

讓我知道如果你有這個問題

+0

謝謝你的時間,但我需要保持代碼接近我有。它花了一點點,但我知道了。再次感謝您的時間。 – user1843918