1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Auto Select in VB List Box

Discussion in 'Visual Basic [VB]' started by pradeep, Apr 23, 2007.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    When a List Box control has the focus, it will automatically scroll to the first item that begins with the letter you type. It will not, however, automatically select items based on more than the first letter. For example, if you type go, you may want the control to select the first item that begins with go. This tip explains how you can implement this behaviour using the API function SendMessage. Its declaration, which must be included in the program, is as follows:
    Code:
    Public Declare Function SendMessage Lib "user32" _
     Alias "SendMessageA" (ByVal hwnd As Long, _
     ByVal wMsg As Long, ByVal wParam As Long, _
     ByVal lParam As String) As Long
    You'll also need the following constant, which tells the List Box control to select the first item that begins with the specified prefix:
    Code:
    Public Const LB_SELECTSTRING = &H18C
    In addition to the List Box, this technique requires a Text Box control. The user enters text in the Text Box, and the List Box automatically selects the first matching item. The message is sent in the Text Box's Change event procedure:

    Code:
    Private Sub Text1_Change()
     
       If Text1.Text <> "" Then
         SendMessage List1.hwnd, LB_SELECTSTRING, -1, Text1.Text
       End If
     
    End Sub
    Letting the user select List Box items based on more than just the first letter can be particularly useful when the list contains many items.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    I have it in the combo box and will soon be adding it here.
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    All you need to do is assign the DroppedDown property to true.

    Something like

    Code:
    cmbName.DroppedDown=true;
    And then it will take care of the issue of what you type gets selected. But of course the prerequisite is to have a editable combo box.
     
  4. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    I guess VB doesn't have editable combo boxes!
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    It has.
     
  6. msubears

    msubears New Member

    Joined:
    Jun 3, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I have a question concerning a gui. I am trying to get a selected text in a textbox to be slecected with either a swipe of a card, or a click of a button. This text will then be printed to a printer. This seems complicated, and i am trying to figure it out.

    If anyone could help that would be great.

    again, the text in the text box all goes to an access database...but through a search function the database comes back and filles in the appropriate text box fields. when that search happens, i need certain text to be captured. when the text is captuerd, maybe it can be pasted into a word doccument or notepad, to be automatically printed?

    Thanks again. :sosp:
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    If you jump into an year old thread with your query I doubt any one would even bother to look at it and reply
     

Share This Page