Consulting

Results 1 to 7 of 7

Thread: Sleeper: Filter ListBox Items

  1. #1
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location

    Exclamation Sleeper: Filter ListBox Items

    Hello all!

    How do I filter the data in ListBox1 using text entered in TextBox1?
    eg. enter "o" will focus on first string start with "o" in ListBox.

    Thanks.

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Try something like this.


    Private Sub CommandButton1_Click()
    Dim i               As Long
    Dim n               As Long
    Dim Str             As String
    Str = Me.TextBox1.Text
        n = Me.ListBox1.ListCount
        For i = 0 To n - 1
            If Left(Me.ListBox1.List(i), Len(Str)) = Str Then
                Me.ListBox1.ListIndex = i
                Exit Sub
            End If
        Next i
    End Sub

    You could also put this code on the change event for the TextBox.


    Private Sub TextBox1_Change()
    Dim i               As Long
    Dim n               As Long
    Dim Str             As String
        Str = Me.TextBox1.Text
        n = Me.ListBox1.ListCount
        For i = 0 To n - 1
            If Left(Me.ListBox1.List(i), Len(Str)) = Str Then
                Me.ListBox1.ListIndex = i
                Exit Sub
            End If
        Next i
    End Sub

  3. #3
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location

    Lightbulb

    Thanks, Killian!



    Is there anywhere that I can focus that filtered data at the uppermost part of that region?

    eg. Type 'o' in tb.

    ListBox1 (requirement that i need)

    ......
    orange <-----
    mango
    apple
    ......


    this type is ur code output, very good!
    (but not I want to implement )...

    ListBox1

    .......
    apple
    mango
    orange <-----
    ........

    Thanks. Anyone can help?

    Or a better idea here...Filter and show only those data start with 'o' (example on top)

    ListBox1 (only show two data [all with o] in the listbox)

    orange <-----
    oxygen


    thanks.

  4. #4
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location
    Can anyone have other ideas on implement filter options on Userform1.ListBox1 ? My seems not working....

    Thanks.

  5. #5
    VBAX Expert xCav8r's Avatar
    Joined
    May 2005
    Location
    Minneapolis, MN, USA
    Posts
    912
    Location
    If you have Visual Studio tools for MS Office, then it would be easier to do what you'd like to do. If not, the easiest workaround that I see is to convert your listbox to a combobox, since you want combobox behavior on your listbox.
    It might not look as good, but functionality always outweighs aesthetics--unless you're doing work for graphic designers.

  6. #6
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location
    functionality always outweighs aesthetics
    Thanks for helping. Now i know a more bout functionality.

  7. #7
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location
    Quote Originally Posted by sheeeng
    Thanks, Killian!



    Is there anywhere that I can focus that filtered data at the uppermost part of that region?

    eg. Type 'o' in tb.

    ListBox1 (requirement that i need)

    ......
    orange <-----
    mango
    apple
    ......


    this type is ur code output, very good!
    (but not I want to implement )...

    ListBox1

    .......
    apple
    mango
    orange <-----
    ........

    Thanks. Anyone can help?

    Or a better idea here...Filter and show only those data start with 'o' (example on top)

    ListBox1 (only show two data [all with o] in the listbox)

    orange <-----
    oxygen


    thanks.
    Sorry for posting again. I really need to know how to filter the ListBox (the selected first data at the top of list box)?
    Does anyone have ideas?
    Can share with me?

Posting Permissions

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