Results 1 to 7 of 7

Thread: [RESOLVED] Insensative Listbox search

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Resolved [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

  2. #2
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: Insensative Listbox search

    Hi,

    If you use the ListBox.Findstring method then the search is not Case sensitive.

    vb Code:
    1. Private Sub FindMyString(ByVal searchString As String)
    2.    ' Ensure we have a proper string to search for.
    3.    If searchString <> String.Empty Then
    4.       ' Find the item in the list and store the index to the item.
    5.       Dim index As Integer = listBox1.FindString(searchString)
    6.       ' Determine if a valid index is returned. Select the item if it is valid.
    7.       If index <> -1 Then
    8.          listBox1.SetSelected(index, True)
    9.       Else
    10.          MessageBox.Show("The search string did not match any items in the ListBox")
    11.       End If
    12.    End If
    13. End Sub
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    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.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: Insensative Listbox search

    I think the best way is to do .ToLower on both strings.

  5. #5
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Insensative Listbox search

    Quote Originally Posted by phpman View Post
    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

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: Insensative Listbox search

    Thanks Kevin, that is what I did, used ToLower instead of ToUpper. but thanks for the idea.

  7. #7
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Insensative Listbox search

    Quote Originally Posted by phpman View Post
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width