如何在ListBox中搜索文本?

时间:2015-11-05 23:30:13

标签: vb.net winforms

所以这就是我的程序看起来

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

答案 0 :(得分: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