views:

1223

answers:

3

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 combobox has 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.

Thanks, G.

+6  A: 

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.

Simon Steele
+1  A: 

A concise version:

const int index = 0;
m_comboBox.PostMessage(CBN_SELCHANGE, index);
Mark Ingram
A: 

What might be going wrong is the selection is being changed inside the selection change message handler, which result in another selection change message.

One way to get around this unwanted feedback loop is to add a sentinel to the select change message handler as shown below:

void onSelectChangeHandler(HWND hwnd)
{
  static bool fInsideSelectChange = 0;

  //-- ignore the change message if this function generated it
  if (fInsideSelectChange == 0)
  {
    //-- turn on the sentinel
    fInsideSelectChange = 1;

    //-- make the selection changes as required
    .....

    //-- we are done so turn off the sentinel
    fInsideSelectChange = 0;
  }
}
jussij