如果列表框包含LIKE项

时间:2013-12-27 01:17:27

标签: vb.net

是否可以在任何地方检查列表框是否包含与字符串类似的项目?

我尝试使用Like语句,但这不起作用。

我试过了:

For i As Integer = 0 To TopicListBox.Items.Count - 1
                If (TopicListBox.Items(i).ToString.Contains(Record.Item("Keyphrase2"))) Then
                    Dim Item As String = TopicListBox.Items(i).ToString
                    If Item Like "Questions related to:" & Record.Item("Keyphrase2") & ": |*| Qs" Then
                        Dim ItemSplit() As String = Item.Split(New Char() {"|"c})
                        Dim AmountOfQuestionsAfter As Integer = AmountOfQuestions + CInt(ItemSplit(1))
                        Item = ItemSplit(0) & AmountOfQuestionsAfter & ItemSplit(1)
                    Else
                        TopicListBox.Items.Add("Questions related to:" & Record.Item("Keyphrase2") & ": |" & AmountOfQuestions & "| Qs")
                        Exit For
                    End If
                End If
            Next

3 个答案:

答案 0 :(得分:2)

我真的不明白你想要完成什么,但这里有一个LINQ函数来返回包含某个字符串的所有项目。

Dim lb As New ListBox

lb.Items.Add("asdf")
lb.Items.Add("asdfasdf")
lb.Items.Add("aifisasdf")
lb.Items.Add("adf")

'' find all items in the ListBox that contain the string "asdf"
Dim found = lb.Items.Cast(Of String).Where(Function(f) f.Contains("asdf"))

For Each s In found
  '' do something with those items
Next

答案 1 :(得分:0)

你不能这样做吗

If Item.Contains("Questions related to:" & Record.Item("Keyphrase2")) Then
    ...
End If

If Item.StartsWith("Questions related to:" & Record.Item("Keyphrase2")) Then
    ...
End If

答案 2 :(得分:0)

Dim Found = (From Result In lb.Items Where Result.ToString = "Value" Select Result)

If (Found.Count > 0) Then
    ' your code
End If