开发者

search through listbox using comma separated values in textbox vb.net 2008

I am writing a code to search through the entire listbox items and highlight them whenever user enters text in textbox. I am looping through textbox items which are entered using a 'comma' . But the code fails to add it to selected indices when user types multiple items using comma. It works fine for single items.

Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
        If (e.KeyCode = Keys.Enter) Then

            ListBox1.BeginUpdate()
            ListBox1.SelectedIndices.Clear()

            If TextBox1.Text.Length > 0 Then
                Dim lstOfStrings() As String = TextBox1.Text.Split(","c)
                For Each s As String In lstOfStrings
                    For index As Integer = 0 To ListBox1.Items.Count - 1

                        If s.Trim() <> "" Then

                            Dim item As String = ListBox1.Items(index).ToString()

 If item.IndexOf(TextBox1.Text, StringComparison.CurrentCultureIgnoreCase) >= 0 Then

                                ListBox1.SelectedIndices.Add(index)

                            End If
                        End If
                    Next
                Next s
                End
 开发者_Python百科               If True Then

                End If


            End If

            ListBox1.EndUpdate()

I think I am missing correct loops or anything else?

Please help.

Thanks.


You are comparing using TextBox1.Text instead of your 'For Each' variable s

The line

If item.IndexOf(TextBox1.Text, StringComparison.CurrentCultureIgnoreCase) >= 0 Then

should be changed to

If item.IndexOf(s, StringComparison.CurrentCultureIgnoreCase) >= 0 Then


Also make sure you Listbox has the property SelectionMode changed to Multi instead of the default of "One"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜