Results 1 to 5 of 5

Thread: [RESOLVED] check if combobox item contains a string

  1. #1

    Thread Starter
    Member ts96's Avatar
    Join Date
    Mar 2010
    Posts
    37

    Resolved [RESOLVED] check if combobox item contains a string

    Hello guys. I would like to know if there is a code to check if a combobox item contains a string and then display only the combobox items that contain the string (like webbrowsers do with the adress combobox). I have found some solutions using instr but I would like someone to help me display these items in combobox (how to make them visible) Here is some code:

    Code:
    Private Sub Combo1_KeyPress(KeyAscii As Integer)
    SendMessage Combo1.hwnd, CB_SHOWDROPDOWN, True, ByVal 0
    Dim i As Integer
    For i = 0 To Combo1.ListCount - 1
    'the code i need 
    Next i
    End Sub

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: check if combobox item contains a string

    I think you are asking about the "Auto Complete".

    Here are some solutions, after doing a search on this forum:

    * Auto Completing ComboBox without API
    * VB6 - Auto Complete Combo Box.
    * [RESOLVED] RESOLVED - Combo Box AutoComplete from access database

    ...

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  3. #3

    Thread Starter
    Member ts96's Avatar
    Join Date
    Mar 2010
    Posts
    37

    Re: check if combobox item contains a string

    Thanks for your reply, but that's not what I'm saying. I want to have a code to display the combobox items i want. Have a look at the folowing code:

    Code:
    Option Explicit
    Private Const CB_SHOWDROPDOWN = &H14F
    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
    
    'I have developed the code viewing by akhileshbc's links
    Private Sub AdressBar_KeyDown(KeyCode As Integer, Shift As Integer)
    Dim i As Integer
    Dim Found
    Select Case KeyCode
    Case vbKeyLeft, vbKeyRight, vbKeyUp, vbKeyDown, vbKeyShift, vbKeyEscape, vbKeyPageUp, vbKeyPageDown, vbKeyControl
        Exit Sub
    Case Else
    SendMessage Combo1.hwnd, CB_SHOWDROPDOWN, True, ByVal 0
    For i = 0 To Combo1.ListCount - 1
    found = InStr(1, AdressBar.List(i), AdressBar.Text, vbBinaryCompare)
    If found <> 0 Then
    'display these item
    Next i
    End Select
    End Sub

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: check if combobox item contains a string

    To show only the matching items, remove everything that doesn't match.

    To do that, replace this:
    Code:
    For i = 0 To Combo1.ListCount - 1
    found = InStr(1, AdressBar.List(i), AdressBar.Text, vbBinaryCompare)
    If found <> 0 Then
    'display these item
    Next i
    ...with this:
    Code:
    For i = Combo1.ListCount - 1 To 0 Step -1
      found = InStr(1, AdressBar.List(i), AdressBar.Text, vbBinaryCompare)
      If found = 0 Then
        AdressBar.Remove i
      End If
    Next i
    However, this will clearly reduce the amount of items each time - so if the amount of characters in AdressBar.Text reduces (or one or more of the characters in it are changed), you will need to re-load the list before re-running this loop.

  5. #5

    Thread Starter
    Member ts96's Avatar
    Join Date
    Mar 2010
    Posts
    37

    Re: check if combobox item contains a string

    Thanks for the code.

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