1

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?

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
martin_ljchan
  • 1,063
  • 1
  • 9
  • 22
  • 2
    `ON_CBN_SELENDOK` – zett42 Jul 11 '19 at 10:59
  • That's exactly it! thanks! Could you make this an answer so I can accept it? – martin_ljchan Jul 11 '19 at 13:42
  • Possible duplicate of [Catching when user selects an item from a CComboBox](https://stackoverflow.com/questions/698910/catching-when-user-selects-an-item-from-a-ccombobox) – zett42 Jul 11 '19 at 18:51
  • @zett42 Interestingly, the top answer there adds some interaction with a CEdit which is not needed in my case. I'm guessing their combobox uses the dropdown style, while mine uses drop list. Does this count as a duplicate? – martin_ljchan Jul 12 '19 at 04:40

1 Answers1

2

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

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164