Search code examples
c++winapimfccomboboxpostmessage

Programmatically change combobox


I need to update a combobox with a new value so it changes the reflected text in it. The cleanest way to do this is after the comboboxhas been initialised and with a message.

So I am trying to craft a postmessage to the hwnd that contains the combobox.

So if I want to send a message to it, changing the currently selected item to the nth item, what would the postmessage look like?

I am guessing that it would involve ON_CBN_SELCHANGE, but I can't get it to work right.


Solution

  • You want ComboBox_SetCurSel:

    ComboBox_SetCurSel(hWndCombo, n);
    

    or if it's an MFC CComboBox control you can probably do:

    m_combo.SetCurSel(2);
    

    I would imagine if you're doing it manually you would also want SendMessage rather than PostMessage. CBN_SELCHANGE is the notification that the control sends back to you when the selection is changed.

    Finally, you might want to add the c++ tag to this question.