Creat a Combo Box & default first entry

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
Who is Participating?
HooKooDooKuConnect With a Mentor Commented:
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.
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).
rayluvsAuthor Commented:
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?

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.
rayluvsAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.