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

<p>So I am trying to craft a <code>postmessage</code> to the hwnd that contains the <code>combobox</code>.</p>

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

<p>I am guessing that it would involve <code>ON_CBN_SELCHANGE</code>, but I can't get it to work right.</p>

[{'answer_id': 59317, 'author': 'Simon Steele', 'author_id': 4591, 'author_profile': 'https://Stackoverflow.com/users/4591', 'pm_score': 4, 'selected': True, 'text': '<p>You want <a href="http://msdn.microsoft.com/en-us/library/bb856484(VS.85).aspx" rel="nofollow noreferrer">ComboBox_SetCurSel</a>:</p>\n\n<pre><code>ComboBox_SetCurSel(hWndCombo, n);\n</code></pre>\n\n<p>or if it\'s an MFC CComboBox control you can probably do:</p>\n\n<pre><code>m_combo.SetCurSel(2);\n</code></pre>\n\n<p>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 <em>back to you</em> when the selection is changed.</p>\n\n<p>Finally, you might want to add the c++ tag to this question.</p>\n'}, {'answer_id': 59350, 'author': 'Mark Ingram', 'author_id': 986, 'author_profile': 'https://Stackoverflow.com/users/986', 'pm_score': 1, 'selected': False, 'text': '<p>A concise version:</p>\n\n<pre><code>const int index = 0;\nm_comboBox.PostMessage(CBN_SELCHANGE, index);\n</code></pre>\n'}, {'answer_id': 98203, 'author': 'jussij', 'author_id': 14738, 'author_profile': 'https://Stackoverflow.com/users/14738', 'pm_score': 0, 'selected': False, 'text': '<p>What might be going wrong is the selection is being changed inside the selection change message handler, which result in another selection change message.</p>\n\n<p>One way to get around this <em>unwanted feedback loop</em> is to add a sentinel to the select change message handler as shown below:</p>\n\n<pre><code>void onSelectChangeHandler(HWND hwnd)\n{\n  static bool fInsideSelectChange = 0;\n\n  //-- ignore the change message if this function generated it\n  if (fInsideSelectChange == 0)\n  {\n    //-- turn on the sentinel\n    fInsideSelectChange = 1;\n\n    //-- make the selection changes as required\n    .....\n\n    //-- we are done so turn off the sentinel\n    fInsideSelectChange = 0;\n  }\n}\n</code></pre>\n'}, {'answer_id': 45231288, 'author': 'serup', 'author_id': 3990012, 'author_profile': 'https://Stackoverflow.com/users/3990012', 'pm_score': -1, 'selected': False, 'text': '<p>if you fx want to change the title - which is the line shown when combobox is closed, then you can do following:</p>\n\n<p>m_ComboBox.DeleteString(0); // first delete previous if any, 0 = visual string\nm_ComboBox.AddString(_T("Hello there"));</p>\n\n<p>put this in fx. in OnCloseupCombo - when event close a dropdownbox fires </p>\n\n<pre><code>ON_CBN_CLOSEUP(IDC_COMBO1, OnCloseupCombo)\n</code></pre>\n\n<p>This change is a new string not a selection of already assigned combobox elements</p>\n'}]
['https://Stackoverflow.com/questions/59280', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/342/']