Results 1 to 6 of 6

Thread: [RESOLVED] Search on Combobox,

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2007
    Location
    Brunei Darussalam
    Posts
    50

    Resolved [RESOLVED] Search on Combobox,

    i have one text box, a combox and a command button,


    Option Explicit
    Private Const CB_FINDSTRING = &H14C
    Private Const LB_FINDSTRING = &H18F
    Private Declare Function SendMessage Lib _
    "user32" Alias "SendMessageA" (ByVal _
    hwnd As Long, ByVal wMsg As Long, _
    ByVal wParam As Long, lParam As Any) _
    As Long

    Private Sub Command1_Click()
    FindCB Combo1, Text1.Text
    End Sub

    Private Sub Form_Load()


    Combo1.AddItem "MB489LE/"
    Combo1.AddItem "MB489XE"
    Combo1.AddItem "MB489FD"
    Combo1.AddItem "MC2341"
    Combo1.AddItem "XDMD42"
    Combo1.AddItem "XDMD41"
    Combo1.AddItem "KCLSISD"

    End Sub

    Private Sub FindCB(obj As Object, TextToFind As String)
    obj.ListIndex = SendMessage( _
    obj.hwnd, CB_FINDSTRING, -1, ByVal _
    TextToFind)
    End Sub
    now the scenario is if i search on MB489 the output will only show the MB489FD which is i think because the Letter comes after the 9 is the F which is base on abcdefg comes first than the other lists on the combo item (am not sure with that) then the search will stop on it...

    i just want to happen that for example i search MB489 all the files on the combo with that name will be shown, i mean if i press Search button, the first file MB489FD will shown up, then if i press again the search button, the MB489LE will again comes up, and so on...

    with code the code above it stop on the first search
    can you please edit the code and add that thing? if its possible?

    TIA

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Search on Combobox,

    It will show MB489FD only if your combo box's property .Sorted =True

    If it is false then the first find will be MB489LE/

    One more thing that you can do is as you type in the text box, the items will be highlighted in the combo box for example...

    Code:
    Option Explicit
    
    Private Const WM_SETREDRAW = &HB
    Private Const KEY_A = 65
    Private Const KEY_Z = 90
       
    #If Win32 Then
        Private Declare Function SendMessage Lib "user32" Alias _
        "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
        ByVal wParam As Long, lParam As Long) As Long
    #Else
        Private Declare Function SendMessage Lib "User" ( _
        ByVal hwnd As Integer, ByVal wMsg As Integer, _
        ByVal wParam As Integer, lParam As Any) As Long
    #End If
    
    Private Sub Form_Load()
        Combo1.AddItem "MB489LE/"
        Combo1.AddItem "MB489XE"
        Combo1.AddItem "MB489FD"
        Combo1.AddItem "MC2341"
        Combo1.AddItem "XDMD42"
        Combo1.AddItem "XDMD41"
        Combo1.AddItem "KCLSISD"
    End Sub
    
    Private Sub Text1_Change()
        Dim sComboText As String, iLoop As Integer
        Dim sTempString As String, lReturn As Long
         
        lReturn = SendMessage(Combo1.hwnd, WM_SETREDRAW, False, 0&)
        
        sTempString = Text1.Text
    
        For iLoop = 0 To (Combo1.ListCount - 1)
            If UCase((sTempString & Mid$(Combo1.List(iLoop), _
            Len(sTempString) + 1))) = UCase(Combo1.List(iLoop)) Then
               Combo1.ListIndex = iLoop
               Combo1.Text = Combo1.List(iLoop)
               Combo1.SelStart = Len(sTempString)
               Combo1.SelLength = Len(Combo1.Text) - (Len(sTempString))
               sComboText = sComboText & Mid$(sTempString, Len(sComboText) + 1)
               Exit For
            Else
                If InStr(UCase(sTempString), UCase(sComboText)) Then
                    sComboText = sComboText & Mid$(sTempString, Len(sComboText) + 1)
                    Combo1.Text = sComboText
                    Combo1.SelStart = Len(Combo1.Text)
                Else
                    sComboText = sTempString
                End If
            End If
        Next iLoop
        lReturn = SendMessage(Combo1.hwnd, WM_SETREDRAW, True, 0&)
    End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2007
    Location
    Brunei Darussalam
    Posts
    50

    Re: Search on Combobox,

    oh sorry been busy...

    i will try your code sir,
    and thanks so much for the time

    regards

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Search on Combobox,

    You could also do this.

    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    FindCB Combo1, Text1.Text
    End Sub
    
    Private Sub Form_Load()
    
    
    Combo1.AddItem "MB489LE/"
    Combo1.AddItem "MB489XE"
    Combo1.AddItem "MB489FD"
    Combo1.AddItem "MC2341"
    Combo1.AddItem "XDMD42"
    Combo1.AddItem "XDMD41"
    Combo1.AddItem "KCLSISD"
    
    End Sub
    
    Private Sub FindCB(obj As Object, TextToFind As String)
    
        Dim lngIndex As Long
        
        Combo1.ListIndex = -1
        
        For lngIndex = 0 To Combo1.ListCount - 1
            If If InStr(1, UCase(Combo1.List(lngIndex)), UCase(Text1.Text)) _
              And Combo1.ItemData(lngIndex) <> 1 Then
                Combo1.ListIndex = lngIndex
                Combo1.ItemData(lngIndex) = 1
                Exit Sub
            End If
        Next
    
        For lngIndex = 0 To Combo1.ListCount - 1
            Combo1.ItemData(lngIndex) = 0
        Next
        
    End Sub
    The second For/Next loop in the FindCB sub allows you to do the search again from the beginning after all the entries have been found. If you want to be able to search a second time before all the matches have been found you'll need to do that code someplace else.

  5. #5

    Thread Starter
    Member
    Join Date
    Dec 2007
    Location
    Brunei Darussalam
    Posts
    50

    Re: Search on Combobox,

    Quote Originally Posted by MartinLiss View Post
    You could also do this.

    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    FindCB Combo1, Text1.Text
    End Sub
    
    Private Sub Form_Load()
    
    
    Combo1.AddItem "MB489LE/"
    Combo1.AddItem "MB489XE"
    Combo1.AddItem "MB489FD"
    Combo1.AddItem "MC2341"
    Combo1.AddItem "XDMD42"
    Combo1.AddItem "XDMD41"
    Combo1.AddItem "KCLSISD"
    
    End Sub
    
    Private Sub FindCB(obj As Object, TextToFind As String)
    
        Dim lngIndex As Long
        
        Combo1.ListIndex = -1
        
        For lngIndex = 0 To Combo1.ListCount - 1
            If If InStr(1, UCase(Combo1.List(lngIndex)), UCase(Text1.Text)) _
              And Combo1.ItemData(lngIndex) <> 1 Then
                Combo1.ListIndex = lngIndex
                Combo1.ItemData(lngIndex) = 1
                Exit Sub
            End If
        Next
    
        For lngIndex = 0 To Combo1.ListCount - 1
            Combo1.ItemData(lngIndex) = 0
        Next
        
    End Sub

    it work with your code sir martin,
    and i guess it suites me best...

    you just accidentally put two IF's
    but it work

    i will save your code and koolsid's codes for future reference,

    thank so much to both of you

    ----and oh----
    another question sir martin,
    guess this will also work with listbox
    i will try to put list box instead of combobox,

    thank you...
    now back to work

  6. #6

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