Creat a Combo Box & default first entry

Posted on 2009-04-24
Last Modified: 2013-11-26
I created a Combo Box in VB6 with various line option for the user to choose


cboOtherReasons.AddItem "<< Choose Option >>"
cboOtherReasons.AddItem "Red Apples"
cboOtherReasons.AddItem "Oranges Stale"

Three things I would like the combop box to do:

1. When the VB app runs, to have "<< Choose Option >>" as the default showned

2. Do not permit the user to write in the Combo Box (that is, alter the fields contents)

4. Auto complete when the user starts typing the name of the option he/she wants
Question by:rayluvs
    LVL 16

    Accepted Solution

    To fulfill option 1, just make sure the "Sorted" property is False, and when the form starts up, the following line of code will select the 1st option:
    cboOtherReasons.ListIndex = 0

    To fulfill option 2, just change the Combo Box "Style" property to "2-Dropdown Combo"

    However, option 4 sort of contradicts option 2.  If the user can not write in the Combo Box, how can you do Auto complete when when the user starts typing the name of the desired option?  Where are they going to type if they can't type in the Combo Box.
    LVL 16

    Expert Comment

    Something LIKE what you are looking for is possible.  If your option list is as short as you've shown, then all you have to do is on the KeyPress event of the Combo Box, search the Combo Box list for the option that starts with the letter they just typed.  If the type "r" or "R", then select "Red Apples".  If they type "o" or "O", then select "Orange Stale".  Anyother key, change the value of the given KeyAscii passed to the KeyPress event to NULL (0) and the combo box will act as if the key wasn't pressed.

    If the option list gets much longer, or if more than one item matches a single key press, things quickly get much more complicated.  It can still be done, but you then have to start getting into properly tracking and changeing the SelStart and SelLength properties of the ComboBox as they key stuff in, you have to add variables to optionally disable certain events at certain times (other wise, in response to one event, you'll do something to the control that will trigger another event that will try to change something that will trigger another event, and suddenly your application crashes because of cascading events), and you have to perform a search through the list for a match if the list of options changes.  If the list gets big, you might have to change to binary search algorithums, etc.

    It can be done.  I did it once a long time ago where I loaded thousands of account numbers into a Combo box, and as the user would key in digits, I was able to find and display the account number that so-far matched what they had typed in, and a binary search pattern kept the interface extreamly responsive (even before the days of multi-processor core CPUs).

    Author Comment

    The first worked like a charm THANX!

    For autocomplete, let rephrase what we need.  The control does sit in this field and by pressing the first letter, it dos get to the item(s) with that letter and when clicking that letter various times, it shows all the options that start with that letter.

    What we need is that as the user types each letter, that the option is displayed.  For example, if there is 3 options:

    Red Apples
    Redden your Breath
    Read Outload

    When the user, in that field, clicks "R"  "E"  "A", we want the field show "Read Outload".  Right now it just show the options only if the first letter is click.

    How can that be done?

    LVL 16

    Expert Comment

    The short answer is something like this...
    On the KeyPress event, determine what the current insert point is on the control (after all, they might have used the mouse to move the insert point).  Take the letters to the left of the insert point, add the new keyed letter, search the list for the 1st option that begins with these letters, select it from the list, then change the insert point to just after the leter thay keyed in (SelStart Property), and change the SelLength property to select to the end of the string.  That way, it looks like the next key press should replace everything to the right of the insert point.
    As a way of letting them know the key they pressed was valid is that if you don't find a match to the letter they keyed in (plus letters to the left of the insert point) then cancel the keypress (set the KeyAscii to 0) and execute Beep.

    In your example (Red Apples, Redden...) you start with an empy ComboBox.  When they type 'r' or 'R', you search the list and find the 1st match (case insensitive) is "Red Apples".  So change the text of the ComboBox to "Red Apples".  Set SelStart to just after the 'R', and set SelLength to select "ed Apples".  They next click "E".  Append "E" to the text in the ComboBox to the LEFT of the SelStart position.  This gives "RE".  Again, a case insensive match finds "Red Apples".  So keep the text as "Red Apples" and set the SelStart between 'e' and 'd' in Red, and set SelLength to select "d Apples".  Now when they press "A", you apend "A" to everything to the left of the SelStart position and get "ReA".  A case-insensitive search matches "Read Outload".  Change the text to "Read Outload", set the SelStart to just after the "a" in "Read" and the SelLength to select "d Outload".  Now, when they press "X", you again take the characters to the left of the SelStart position and add "X" to it to get "ReaX".  You find that that doesn't match anything.  You set KeyAscii = 0 (cancels the "X") and execute "Beep", and leave the text, SelStart, and SelLength where it was.  Keep in mind, you can't track the number of keys that they have pressed, because they could use the mouse to change the SelStart and SelLength on you.  So with each KeyPress event, you have to check the current values of these properties, and allow for the fact that the user can change them to just about anything.

    Also, don't forget about special keys like Back-Space.  In that case, you have to erase everythihng hilighted, remove one character, and do your search all over again.

    Author Comment


    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now