Reputation: 3689
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 :
But when i first open combobox, and then start typing i get something like that:
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..
Upvotes: 7
Views: 21978
Reputation: 337
void cmbExample_KeyDown(object sender, KeyEventArgs e)
{
cmbExample.DroppedDown = false;
}
Upvotes: 0
Reputation: 6577
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.
Upvotes: 12
Reputation: 41
Implement event on ComboBox KeyDown. It should look like this.
void cmbExample_KeyDown(object sender, KeyEventArgs e)
{
if ((sender as ComboBox).DroppedDown)
(sender as ComboBox).DroppedDown = false;
}
Upvotes: 3
Reputation: 21
I also found the default UI implementation distracting as the two dropdowns fight for mouse control.
You want to hide the dropdown list when autocomplete suggestions are shown. There is a windows message that the combobox makes before showing the autocomplete suggestions. I chose to collapse the droplist in response to this message. It requires a small override of the combobox to achieve this:
Public Class Combobox2
Inherits ComboBox
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = 135 AndAlso DroppedDown Then 'WM_GETDLGCODE
DroppedDown = False
End If
MyBase.WndProc(m)
End Sub
End Class
Upvotes: 2
Reputation: 31
I was having exactly the same problem. I tried the DropDown and DropDownClosed events to set the AutoCompleteMode property to none and suggest. In this situation the SelectedIndexChanged event does not get fired after selecting an item with the mouse. I was using the SelectedValue property in the SelectedIndexChanged event and this property is already changed at the moment the DropDownClosed event is triggered. In my case I simply called the SelectedIndexChanged method from the DropDownClosed event to solve the problem.
Upvotes: 3
Reputation: 420
What about using the DropDown and DropDownClosed events to disable or change the auto-complete mode?
Upvotes: 5
Reputation: 32851
Have you tried the other possible values for AutoCompleteMode
, which are Append
, None
, and Suggest
? I think that what you are looking for is Suggest
instead of AppendSuggest
.
Here is some downloadable sample code illustrating the different modes, if you need it.
Upvotes: 2