简体   繁体   中英

Why can't I add a string to a combo box?

This seems trivial, but with MFC I always end up with some stupid trivial problem that puts a stop to my workflow.

I am getting a "Debug Assertion Failed" error pointing to afxcmn2.inl line 352:

_AFXCMN_INLINE int CComboBoxEx::AddString(LPCTSTR lpszString)
    { UNUSED_ALWAYS(lpszString); ASSERT(FALSE); return CB_ERR;}

I am attempting to just add some strings to a combo box on initialization like so:

BOOL myDialog::OnInitDialog()
{
    CDHtmlDialog::OnInitDialog();
    cb_direction.AddString(CString("North"));
}

Most of the answers on Google seem to suggest that the AddString is happening before OnInitDialog, which doesn't seem to be the case here. Another series of answers on Google suggests the data exchange isn't happening or it's wrong, but it's not:

void myDialog::DoDataExchange(CDataExchange* pDX)
{
    CDHtmlDialog::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_WHEDIT_DIR, cb_direction);
}

Another suggestion was that the combo box hasn't been created yet, but if I disable the combobox using the following code, not only do I NOT get an error, but it actually works and disables the box!

BOOL myDialog::OnInitDialog()
{
    CDHtmlDialog::OnInitDialog();
    cb_direction.EnableWindow(FALSE);
}

I've cleaned the solution and rebuilt it. I'm not sure what else I am missing. And all I want to do is to add a string to a combo box, which would take 2 seconds in .Net (this program that was written years ago by someone else which is why it's in MFC rather than .Net, but I digress).

Entering the game a little late but, who knows, this might help someone someday:

  COMBOBOXEXITEM  item;
  ZeroMemory(&item, sizeof(item));
  item.mask = CBEIF_TEXT;
  item.iItem = 0;
  item.pszText = _T("Hello");
  m_ComboEx.InsertItem(&item);

FWIW, AddString() functionality is removed from CComboEx because the purpose of the control is to display advanced items (with images, identation, whatever...), not straight regular text items.

Well if you look at what the method is doing they have an ASSERT(FALSE) in there, so no wonder. It doesn't actually do anything that would indicate it adds an item to the ComboBoxEx control. Per the docs

This function is not supported by the Windows ComboBoxEx control. For more information on this control, see ComboBoxEx Controls in the Platform SDK.

The documentation is your friend :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM