2015-11-05 83 views
1

因此,這裏是我的計劃看起來是什麼如何搜索ListBox中的文本?

Click this

什麼我想要做的就是搜索多少次也有一年級

這裏是代碼的朋友幫我做

Private Function countOccOfMark(ByVal mark As String) 
    Dim resultCount As Integer = 0 

    For cnt As Integer = 0 To lstDisplay.Items.Count - 1 
     Dim gradeMark As String = lstDisplay.Items(cnt) 
     Dim results() As String = gradeMark.Split(vbTab) 

     For Each res As String In results 
      If (res = mark) Then 
       resultCount = resultCount + 1 
      End If 
     Next 
    Next 

    Return resultCount 
End Function 


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Dim mark As String = InputBox("Enter a Grade to search for") 
    Dim amountFound As Integer = countOccOfMark(mark) 
    MessageBox.Show("Found " & amountFound & " Occurences of the mark " & mark) 
End Sub 

哪些工作正常,但我敢肯定有一個更簡單的方法來做到這一點

+1

A)listview將允許您顯示在適當的列中的內容,以便您不需要拆分數據來檢查它。 2)一個持有名稱,等級百分比的類可以顯示如圖所示(使用'ToString()',但分開保存數據,所以你不需要拆分數據來檢查它 – Plutonix

回答

1

內部For-Next是不必要的。等級總是在第二列。

Private Function countOccOfMark(ByVal mark As String) 
    Dim resultCount As Integer = 0 

    For cnt As Integer = 0 To lstDisplay.Items.Count - 1 
     Dim gradeMark As String = lstDisplay.Items(cnt) 
     Dim results() As String = gradeMark.Split(vbTab) 
     If results(1) = mark Then 
      resultCount = resultCount + 1 
     End If 
    Next 

    Return resultCount 
End Function 
+0

我知道還有另一種方式來做到這一點!謝謝你,你是一個很好的幫助 – Thomas06501

+0

@ Thomas06501不客氣,如果這樣做,請接受我的回答爲「回答」 - 除非你想要保持對話的開放。 – JerryM