Reputation: 513
I have a ComboBox
whose DropDownStyle
is DropDown
, allowing the user to type into it and its AutoCompleteMode
is Suggest
. The problem is that if the ComboBox
is currently open and the user starts typing into it the auxiliary drop-down list appears but clicking on an item from it actually selects the item from the ComboBox's original drop-down list residing under the mouse at the time of click.
I would prefer if while the ComboBox's drop-down list is open the user could not type into it and would like to know if there is a more elegant solution than:
AutoCompleteMode
to None
when the ComboBox
is openDropDownStyle
to DropDownList
on the OnClick
event (haven't tried, but the theory is sound)Upvotes: 5
Views: 3973
Reputation: 125277
As an option you can handle KeyPress
event of the ComboBox
and close the dropdown. It keeps the autocomplete menu open, but closes dropdown:
private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
this.comboBox1.DroppedDown = false;
}
As another option, you can handle DropDown
and DropDownClosed
events of the ComboBox
and disable autocomplete in DropDown
and enable it again in DropDownClosed
event:
private void comboBox1_DropDown(object sender, EventArgs e)
{
this.comboBox1.AutoCompleteMode = AutoCompleteMode.None;
}
private void comboBox1_DropDownClosed(object sender, EventArgs e)
{
this.comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
}
You can create a new class deriving from ComboBox
and override corresponding OnXXXX
methods and put the logic there. This way you encapsulate the fix in the control class instead of handling events in your form and will have a reusable bug-free control and a more clean code.
Upvotes: 9