3 Replies - 953 Views - Last Post: 15 March 2011 - 05:11 AM Rate Topic: -----

#1 akramwahid  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 14-March 11

Working with combo box

Posted 14 March 2011 - 06:45 PM

Andy Bates

Posts: n/a
#1: Nov 16 '05

I've got what at first appears to be a really simple problem.

I have a dropdown combobox with a list of names in, the user can enter text
into the edit portion or drop the combobox and select a name.

What I need to do is to drop the combobox when the user types and find the
nearest entry to what the user entered (so if they typed A, Andy may be the
nearest entry, the list should scroll, but not select, unless the user uses
arrow down or clicks with the mouse). This should progress this way as the
user enters more text.

I can use the DroppedDown property to drop the combobox, but if I do this in
the TextChanged event, then the nearest item in the combobox is selected and
the text is put into the edit box. I've played around with various state
variables, but none of them give me the functionality I require.

The same problem was solved previously in C++, but can I get the C# code to

Any help would be greatly appreciated?

Is This A Good Question/Topic? 0
  • +

Replies To: Working with combo box

#2 scalt  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 342
  • Joined: 22-November 07

Re: Working with combo box

Posted 14 March 2011 - 08:30 PM

I may have a partial solution to your problem. This code will 'select' the item, then highlight the remainder of the text so that the next keypress the user does will overwrite it if they want to keep typing (alternatively they may hit 'down' or 'enter' to select). However this will not highlight the entry in the combo box, as soon the line that selects the remainder of the text is called it seems to lose the highlight. If the user tries to use the arrows keys to navigate the box then it will behave as if the orrect entry was highlighted, but you can't actually SEE it. Have a play and see if it makes sense. I used 2 event handlers to do it. A keypress handler to see if the backspace key was pressed, if it was then the 'textchanged' handler will essentially ignore the event. If I didn't add this then the user would not be able to delete any characters they had typed, it would just keep reselecting the matching entry and ignoring the backspace key. The 'textchanged' handler just stores the characters the user typed, uses those to find the first matching entry, then does the selection thing.

Hope this helps

 bool processthisedit = false;
        private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
            //if the user has NOT hit the 'backspace' key
            if (e.KeyChar != 8)
                //set the flag that will allow the 'textchanged' event handler
                //to do its thing
                processthisedit = true;

        private void comboBox1_TextChanged(object sender, EventArgs e)
            //if we should be processing this event (as set by the keypress handler)
            if (processthisedit)
                //reset our flag
                processthisedit = false;

                //grab the initial text the user typed
                string text = comboBox1.Text;

                foreach (string entry in comboBox1.Items)
                    //try and find an entry in our combo box that starts with what they typed
                    //(case insensitive)
                    if (entry.ToUpper().StartsWith(text.ToUpper()))
                        //drop our box down
                        comboBox1.DroppedDown = true;

                        //'select' our item
                        comboBox1.SelectedItem = entry;

                        //select the tail end of the text in the combo box (the bit the user hasn't typed yet)
                        comboBox1.Select(text.Length, entry.Length - text.Length);


This post has been edited by scalt: 14 March 2011 - 08:32 PM

Was This Post Helpful? 0
  • +
  • -

#3 [email protected]  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 1002
  • View blog
  • Posts: 975
  • Joined: 30-September 10

Re: Working with combo box

Posted 15 March 2011 - 02:42 AM


Can you not just used the built in .AutoCompleteMode and .AutoCompleteSource properties of the ComboBox control?

You can set the .AutoCompleteSource property to 'ListItems', and set the .AutoCompleteMode to 'Suggest' or 'SuggestAppend', and it will do automatically pretty much what you are trying to do :)

This post has been edited by [email protected]: 15 March 2011 - 02:44 AM

Was This Post Helpful? 1
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Freelance developer
  • member icon

Reputation: 6216
  • View blog
  • Posts: 13,722
  • Joined: 02-June 10

Re: Working with combo box

Posted 15 March 2011 - 05:11 AM

Do you have to let the user enter items NOT in the list?
If you change it from DropDown to DropDownList then typing automatically takes you to the nearest item on the list.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1