martin_ljchan
martin_ljchan

Reputation: 1063

CComboBox event for making selection by enter key?

I have a dialog with a CComboBox in the DropList style. I want it to call my function (e.g. LoadData()) when:

  1. user clicks an item in the drop list, or
  2. an item in the drop list is highlighted (either by mouse hover or keyboard), and user presses enter

but NOT when the user is still typing text in #2.

Calling LoadData() in the ON_CBN_SELCHANGE handler works fine for #1, but for #2 this event fires on every keystroke instead of only on enter. In other words, if I have combobox items:

1
12
123

and I type 12, it will trigger ON_CBN_SELCHANGE once for 1, once for 12 ... but really I'm trying to type 123, so I don't want those first 2 keystrokes to result in LoadData() calls.

What's the correct way to implement this?

Upvotes: 1

Views: 368

Answers (1)

Andrew Truckle
Andrew Truckle

Reputation: 19157

Further to the comments in your question, here is the answer:

  • Right-click the control and select Add Event Handler:

1

  • Select the dialog class, choose the message CBN_SELENDOK (and adjust the method name if you want to):

2

  • This will add default event handler for you:
    void CMFCApplication1Dlg::OnCbnSelendokCombo1()
    {
        // TODO: Add your control notification handler code here
    }

Now you can proceed as required.


Alternative

  • Select the control and then look at the properties pane. Click on Control Events:

3

  • Locate CBN_SELENDOK in the list, click the drop-down arrow and select the option to add the handler:

4

Upvotes: 2

Related Questions