Programmatically change combobox

I need to update comboboxwith a new value so that it changes the reflected text. The cleanest way to do this is after initialization comboboxand with a message.

So, I'm trying to create postmessagefor hwnd containing combobox.

So, if I want to send him a message by changing the current selected item to the nth item, what does it look like postmessage?

I suppose it will turn on ON_CBN_SELCHANGE, but I cannot get it to work correctly.

+2
source share
4 answers

Do you want ComboBox_SetCurSel :

ComboBox_SetCurSel(hWndCombo, n);

MFC CComboBox:

m_combo.SetCurSel(2);

, , SendMessage, PostMessage. CBN_SELCHANGE - , .

, ++ .

+8

:

const int index = 0;
m_comboBox.PostMessage(CBN_SELCHANGE, index);
+1

, , .

, , :

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;
  }
}
0

if you want to change the title - this is the line displayed when closing combobox, you can do the following:

m_ComboBox.DeleteString (0); // first delete the previous one, if any, 0 = visual line m_ComboBox.AddString (_T ("Hello there"));

put it in fx. in OnCloseupCombo - when the event closes, dropdownbox fires

ON_CBN_CLOSEUP(IDC_COMBO1, OnCloseupCombo)

This change is a new line that does not have a selection of already assigned list items.

-1
source

Source: https://habr.com/ru/post/1718415/


All Articles