Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combobox and autocomplete in C#

i have small problem with autocomplete option in combobox. Everything is working correct, except that i want to work it diffrent :)

When I start typing in combobox, autusuggest working the way i like :

Combo

But when i first open combobox, and then start typing i get something like that:

enter image description here

What's more i can't pick item from autosuggest combobox, only from this list under.

AutocompleteMode is SuggestAppend

I'd like to have autosuggest like on the first picture, and in situations like picture 2, this first combobox list should be closed somehow..

like image 630
Elfoc Avatar asked Jun 05 '11 14:06

Elfoc


People also ask

What is AutoComplete ComboBox?

The AutoComplete properties like AutoCompleteCustomSource, AutoCompleteMode and AutoCompleteSource to perform a TextBox that automatically completes user entry strings by comparing the initial letters being entered to the prefixes of all strings in a data source.

What is ComboBox in C sharp?

A ComboBox displays a text box combined with a ListBox, which enables the user to select items from the list or enter a new value. The DropDownStyle property specifies whether the list is always displayed or whether the list is displayed in a drop-down.


2 Answers

I had the same problem and solved it this way:

private void comboBox_DropDown(object sender, EventArgs e)
{
    ComboBox cbo = (ComboBox)sender;
    cbo.PreviewKeyDown += new PreviewKeyDownEventHandler(comboBox_PreviewKeyDown);
}

private void comboBox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    ComboBox cbo = (ComboBox)sender;
    cbo.PreviewKeyDown -= comboBox_PreviewKeyDown;
    if (cbo.DroppedDown) cbo.Focus();
}

Once the user clicks on the DropDown button PreviewKeyDown event is attached to that ComboBox. When user starts typing, freshly added event is triggered. In that event we check if ComboBox is DroppedDown, if it is, focus that ComboBox. On ComboBox focus DropDown disappeares and that's it.

like image 77
msmolcic Avatar answered Sep 16 '22 20:09

msmolcic


What about using the DropDown and DropDownClosed events to disable or change the auto-complete mode?

like image 44
Mike LaSpina Avatar answered Sep 16 '22 20:09

Mike LaSpina