Results 1 to 11 of 11

Thread: Combo Box and typing on keyboard

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Posts
    89

    Cool Combo Box and typing on keyboard

    Hello,

    Is there anyway in VB that if you have a combobox for "Hair color"
    that if you type the 1st letter, the combobox will automatically scroll down and select the hair color?


    For example, here's the list of hair colors:

    Green
    Orange
    Yellow
    Gray
    Black
    Blue


    If I push the button "B" on the keyboard, I would like the blank combo box to select "Black" first. Kind of a shortcut for lazy users that don't want to click on the combobox to make their selection.

    Thanks in advance

    Chris

  2. #2
    Fanatic Member
    Join Date
    Sep 2002
    Location
    Lexington, SC
    Posts
    586
    In your keypress sub do this.

    VB Code:
    1. Dim chrKey as String
    2. Dim I as Long
    3.  
    4. chrKey = chr(keyascii)
    5.  
    6. If Combo.Text = "" then
    7.   For I = 1 to Combo.ListCount
    8.     If Left(Combo.List(I), 1) = chrKey Then
    9.       keyascii = 0
    10.       Combo.Text = Combo.List(I)
    11.       Exit For
    12.     End If
    13.   Next
    14. End If

    That will take will find a match to whatever key you press if there is a match. And it will use the first one found if there are more than one. You might have to change I to be 0 to Listcount - 1.. can't remember the lbound of a combobox offhand.

    It also only works when the text is empty that way if the user is typing something else in the box it wont erase it and put the color in there.

  3. #3

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Posts
    89

    Thanks!

    StevenHickerson,


    That works perfectly, I had to change the I to I = 0....... however, 1 minor thing, do you know how to include either uppercase or lowercase when you do a keypress?

    For now all the colors have a capital letter as the first letter, typing a lower case letter into the combobox doesn't do anything........


    THanks again, and thanks martin for the example

    Chris

  5. #5
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    You could do:
    VB Code:
    1. If UCase(Left(Combo.List(I), 1)) = UCase(chrKey) Then




    And, you will - 1 as illustrated in the following:
    VB Code:
    1. For I = 0 To Combo.ListCount [b]- 1[/b]
    Last edited by Bruce Fox; Nov 26th, 2003 at 06:32 PM.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Posts
    89

    Resolved!

    Worked like a charm bruce, thx for the tip !


    Chris

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Posts
    89

    Resolved plus question?

    One more thing I forgot about, is there anyway that if you type a letter, the combobox opens up and the highlighter goes down to the one you selected?


    Plus: When I type "B" for example, if there's more than 1 "B" in the list of colors, if I push the "down" arrow key, the highlighter goes straight to the first one on the list..... is there a way for the highlighter to continue down the combobox list?


    Thanks again


    Chris

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

    Re: Resolved plus question?

    Originally posted by Trancedified
    One more thing I forgot about, is there anyway that if you type a letter, the combobox opens up and the highlighter goes down to the one you selected?


    Plus: When I type "B" for example, if there's more than 1 "B" in the list of colors, if I push the "down" arrow key, the highlighter goes straight to the first one on the list..... is there a way for the highlighter to continue down the combobox list?


    Thanks again


    Chris
    That's what my example does.

  9. #9
    Hyperactive Member D12Bit's Avatar
    Join Date
    Oct 2000
    Location
    Guatemala
    Posts
    373
    Lol!!...

    I haven't looked at the example of MartinLiss ...

    but I bet that he set the Combobox Style to: 2 - DropdownList

    Wich it autmatically does what you want...it even does it better that "reinventing sweetened water"

    as you type...the Combobox is updated....

    you can have two or more words that start with the same letteres...ans as you type the complete word it will be selected...

    ex.
    (set the Sort property of the Combobox to True)
    list:

    cry
    carriage
    cartoon


    if you type "c" it'll be poiting to cry
    if you type "ca" it'll point carriage
    if you type "car" it'll still be pointing to carriage
    but...
    if you type "cart" then it'll be pointing to cartoon

    and if you press backspace...it'll be returning to the previous selection...according to the letters typed.
    "Who Dares Wins" - "Quien se Arriesga Gana"
    Mail me at:

  10. #10
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Sure, the combobox when style = 2 and sorted = true does what you describe, but the code I posted (which was written by Aaron Young) does more. It allows you to press the backspace key to "take back" the last letter you typed, it allows you to set the time limit before a new search begins and it allows you to decide if you want to hear a beep when no entry matches the typed letter(s).

  11. #11
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Marty's going for gold as usuall i see

    Always one step ahead of us!

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