luke
luke

Reputation: 172

Combobox Autocomplete Tab-out does not select item

So there are a bunch of questions similar to mine, but none exactly what I need. I have a combobox that is populated with a list of cities. I turned on the Autocomplete and that works exactly how i want with the suggestappend also turned on. The problem is, though, if the user tries to tab out of the combo box, it does not actually select the item. here is an example: I am searching for "Orlando". If i type in "orla", the suggestion fills out the rest of the word (selected), so it shows "orlando". So that is the item I want to select. If i hit enter and then tab out, it will select the item and then tab out. What i need though, is for tabbing out to select the underlying item that matches the word. If i need to explain more, I can. Thanks in advance!

Luke

Upvotes: 1

Views: 6223

Answers (3)

Breeze
Breeze

Reputation: 2058

The value gets lost at the WM_KILLFOCUS message. Overriding WndProc in a subclass of ComboBox solved this issue for me. Unfortunately, I have only VB.NET-code at hand:

Protected Overrides Sub WndProc(ByRef m As Message)
    If m.Msg = &H8 Then  'WM_KILLFOCUS
        Dim sText As String = Me.Text
        MyBase.WndProc(m)
        Me.Text = sText
        Exit Sub
    End If

    MyBase.WndProc(m)
End Sub

Upvotes: 0

Jeremy Thompson
Jeremy Thompson

Reputation: 65624

I get the same behaviour as OP and the marked answer (from Albert who is unable to reproduce the problem) isn't a solution. This issue has also been reported to Connect as a Bug:

https://connect.microsoft.com/VisualStudio/feedback/details/711945/tab-on-a-winforms-combobox-with-properties-dropdownstyle-dropdownlist-autocompletemode-append-autocompletesource-listitems-doesnt-work-correctly

I didn't bother creating a custom combobox control as specified in the workaround section of the Connect Bug. Instead I just set the dropdownlist with a default value:

cboAccount.SelectedValue = _accountList(0).Key;   //<--Here I set a default value
cboAccount.DroppedDown = true;

Upvotes: 1

Albert Walker
Albert Walker

Reputation: 331

Which version of .NET are you using? I tried it in 3.5, and the behavior is the opposite of what you describe. When I type a partial name and tab out, it selects the item in the list. If I hit enter, it doesn't select the item, and it actually clears the value I just entered.

How are your properties set on the ComboBox? I have AutoCompleteMode = SuggestAppend and AutoCompleteSource = ListItems.

Upvotes: 1

Related Questions