Archived

This topic is now archived and is closed to further replies.

Corben

MFC dialogs with CComboBoxes

Recommended Posts

So the problem is, how to insert strings to a CComboBox (dropdown list) in an MFC dialog created with the editor. (Visual C++ 5.0, no service packs) I've got the code below where I would need the dialog box. The m_terrainCombo is mapped through ClassWizard to the combo box as a control (CComboBox). ... CLandDialog landDlg; // CComboBox* terrainCombo = (CComboBox*) landDlg.GetDlgItem(1010); // try to get the combo box a different way (gives an assertion error) // landDlg.m_terrainControl.InsertString(0, "Hills"); // as gives this // .. access the other controls.. (works fine) landDlg.DoModal(); ... No matter which of the commented lines I use, it throws an assertion error and refuses to add anything. (The assertion usually is something about m_hWnd, but I don't actually get it, what's it trying to say. I tried to give the dlg a handle to my main wnd, but it didn't change anything.) I've tried to read the docs and the books I have, and according to everything, those both lines should be just ok, so if someone knows what I'm missing here, please reply. -- .. I tried to fix it now again today and I found out that the m_hWnd of the dialog box is always NULL, why is that? and is that normal? And because of that, it always gives an assertion error when trying to do something with the window. How to get around this? Any ideas? Edited by - Corben on 7/20/00 3:39:34 AM

Share this post


Link to post
Share on other sites
Well, I''m not totally sure this is what you''re doing, but from what it looks like, you are attempting to put stuff in the combo box before the combo box technically exists. Your other control stuff probably works because you are probably setting the variables relating to those windows rather than trying to access the windows themselves. This would be why your dialog window handle is NULL... until you DoModal you don''t actually have a window. (and yes, I know this because I''ve made this mistake... *sheepish look).

So what you need to do is make an array or linked list in your dialog box class which will hold the information you want to put into the combo box until the box is actually initialized, and for dialogs I believe populating from OnInit will work fine, don''t quote me on that tho because I don''t have any of that source with me at the moment.

Yell if you have any more problems.

-fel

Share this post


Link to post
Share on other sites
Ah, of course.. that was just the case. The thing was that I thought I had thought of that before, becouse I had tried the lines _after_ the DoModal() too.. Yep, should''ve thought of it for a second. Fortunately now with the CStringList it took only a three lines of code to add the same functionality back.

Thanks!

Share this post


Link to post
Share on other sites
I''m sure you already know this, but just in case you don''t:
The easiest way to put text in a combobox, is to modify the data tab under the combobox properties in the resource view. Just add them one after another here.
Of course they are not dynamic, but it sounds like yours may be fairly fixed. Plus you can add items with code.
I''m not sure if ver.5 has this in it''s resource editor.

Share this post


Link to post
Share on other sites