[RESOLVED] Insensative Listbox search
I am trying to do a search function for a listbox and I have the search working but it is case-sensitive. How do you make it non?
Code:
For i As Integer = 0 To FrmMain.LstBxMovie.Items.Count - 1
If Not cancelled Then
If FrmMain.LstBxMovie.Items(i).ToString.Contains(sSearchString) Then
FrmMain.LstBxMovie.SelectedIndex = i
'MsgBox(FrmMain.tvwMain.SelectedNode)
response = MsgBox("click ok to continue search, or cancel to abort search", MsgBoxStyle.OkCancel Or MsgBoxStyle.DefaultButton1 Or MsgBoxStyle.Question)
If response = MsgBoxResult.Cancel Then cancelled = True
End If
End If
Next
Re: Insensative Listbox search
Hi,
If you use the ListBox.Findstring method then the search is not Case sensitive.
vb Code:
Private Sub FindMyString(ByVal searchString As String)
' Ensure we have a proper string to search for.
If searchString <> String.Empty Then
' Find the item in the list and store the index to the item.
Dim index As Integer = listBox1.FindString(searchString)
' Determine if a valid index is returned. Select the item if it is valid.
If index <> -1 Then
listBox1.SetSelected(index, True)
Else
MessageBox.Show("The search string did not match any items in the ListBox")
End If
End If
End Sub
Re: Insensative Listbox search
anyway to incorporate that into my search function? I want to search for one and continue from there, not just find all.
Re: Insensative Listbox search
I think the best way is to do .ToLower on both strings.
Re: Insensative Listbox search
Quote:
Originally Posted by
phpman
anyway to incorporate that into my search function? I want to search for one and continue from there, not just find all.
The attached VS2008 + project has a method extension that will search, prompt user to continue after each find.
Code:
<System.Runtime.CompilerServices.Extension()> _
Public Sub FindSearchString(ByVal sender As ListBox, ByVal searchString As String)
If String.IsNullOrEmpty(searchString) Then
Exit Sub
End If
searchString = searchString.ToUpper
Dim Items = (From Item In sender.Items.Cast(Of String)() Select Item.ToUpper).ToList
For Row As Integer = 0 To sender.Items.Count - 1
If Items(Row).Contains(searchString) Then
sender.SetSelected(Row, True)
If MsgBox("click ok to continue search, or cancel to abort search", MsgBoxStyle.YesNo) <> MsgBoxResult.Yes Then
Exit Sub
End If
End If
Next
End Sub
Re: Insensative Listbox search
Thanks Kevin, that is what I did, used ToLower instead of ToUpper. but thanks for the idea.
Re: Insensative Listbox search
Quote:
Originally Posted by
phpman
Thanks Kevin, that is what I did, used ToLower instead of ToUpper. but thanks for the idea.
Both do the same in regards to what you wanted. Good to hear all worked out for you.