# Adding Items (Strings) to Combo Box?

This topic is 3847 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey everyone, I seem to be having some trouble adding strings to my combo boxes which is in a Dialog I made (I think its modal, called with the DialogBox() function). Here's my code: Dialog Message Loop:
INT_PTR CALLBACK SettingsBox(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
//UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
{
return (INT_PTR)TRUE;
}
case WM_COMMAND:
if (LOWORD(wParam) == IDCANCEL)
{
PostQuitMessage(0);
gSettingsDone = true;
gSettingsCancel = true;
EndDialog(hDlg, LOWORD(wParam));
DestroyWindow(g_hWnd);
return (INT_PTR)TRUE;
}
if (LOWORD(wParam) == IDOK)
{
EndDialog(hDlg, LOWORD(wParam));
gSettingsDone = true;
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}

LoadSettingsBox() function which populates the ComboBoxes etc:
void LoadSettingsBox(HWND hDlg)
{
//resBox=(CComboBox *)GetDlgItem(hDlg, IDC_RESOLUTION);
//	resBox->SetDlgCtrlID(IDC_RESOLUTION);
HWND hRes = GetDlgItem(hDlg, IDC_RESOLUTION);
SendMessage(hDlg, CB_INSERTSTRING,  0, (LPARAM)"640x480");

//SendDlgItemMessage(hDlg, IDC_RESOLUTION, WM_SETTEXT,  0, (LPARAM)"640x480");
}

As you can see I commented a lot out of the LoadSettingsBox because I couldn't even get one of the strings to be added to the combo box. Nothing appears. Nothing... I've tried manipulating all of the ComboBox properties and everything. The combobox ID is IDC_RESOLUTION. I also tried using MFC, which I've never used before, with the CComboBox class, but looking at the header files of said class shows that it's using the same routine I'm using. Does anyone know why nothing is being added? Anyone got any working examples? I'm at a loss...

##### Share on other sites
When you say combo box, you mean the control that allows you to pull down a list and select a string right? In MFC, you would do something like:

BOOL CEx06aDialog::OnInitDialog() {	CListBox* pLB = (CListBox*) GetDlgItem( IDC_DEPARTMENT );	pLB->InsertString( -1 , "Documentation" );	pLB->InsertString( -1 , "Accounting" );	pLB->InsertString( -1 , "Human Relations" );	pLB->InsertString( -1 , "Security" );	return CDialog::OnInitDialog();}

MSDN documentation

##### Share on other sites
I tried it, but its giving me a JIT debug unhandled exception error:
0xC0000005: Access violation reading location 0x0022048c.

Breaking at the this inline function at
AFXWIN_INLINE int CComboBox::InsertString(int nIndex, LPCTSTR lpszString)	{ ASSERT(::IsWindow(m_hWnd)); return (int)::SendMessage(m_hWnd, CB_INSERTSTRING, nIndex, (LPARAM)lpszString); }
in afxwin2.inl .

I changed it to CComboBox because I was using a combo box instead of a list box (but I got the same result with CListBox too);

Heres my code in context:
INT_PTR CALLBACK SettingsBox(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam){	//UNREFERENCED_PARAMETER(lParam);	switch (message)	{	case WM_INITDIALOG:		{			CComboBox* pLB = (CComboBox*) GetDlgItem(hDlg, IDC_RESOLUTION);			pLB->InsertString( -1 , "Documentation" );			pLB->InsertString( -1 , "Accounting" );			pLB->InsertString( -1 , "Human Relations" );			pLB->InsertString( -1 , "Security" );            LoadSettingsBox(hDlg);			return (INT_PTR)TRUE;		}

I'm stumped...

##### Share on other sites
The version of GetDlgItem you are calling returns an HWND.

You're casting it to a pointer to CComboBox. One is a window handle, and the other is a MFC c++ object. They are *completely* different things. There's a good reason why c-style casts are dangerous :-)

What you're doing in your first post is a little "closer" to correct... but look at what you're sending the message to:
HWND hRes = GetDlgItem(hDlg, IDC_RESOLUTION);SendMessage(hDlg, CB_INSERTSTRING,  0, (LPARAM)"640x480");

You're getting the handle to the combobox (assuming IDC_RESOLUTION is the control id of the combobox), then you're ignoring it and sending a combobox message to the dialog's window handle.

HWND hRes = GetDlgItem(hDlg, IDC_RESOLUTION);SendMessage(hRes, CB_INSERTSTRING,  0, (LPARAM)"640x480");

Step through the code in the debugger too, to make sure hRes is not NULL (which would mean there is no control with control id IDC_RESOLUTION in your dialog).

##### Share on other sites
Hahaha, well I figured casting like that seemed a bit off... but my dad and ahayweh told me to do it like that, but it still isn't working...

How would I point my CComboBox or CListBox objects to know which listbox or combobox I want it to attach to if I made said listbox or combobox in the visual studio 2005 dialog editor?

##### Share on other sites
I wouldn't mix and match MFC and straight win32. It's liable to be confusing.

So since you started with a straight win32 project, you should probably just use SendMessage and HWNDs, rather than the c++ MFC wrapper classes.

I edited my response above to be a little more helpful...

##### Share on other sites
Ahhh Thank You!!! I can't believe I missed that.

Works like a charm now!

I'll take your advice and stick to what I was doing. I'll be sure to learn MFC for my next project, as its seems a bit more manageable and fool-proof.

Thanks again!

##### Share on other sites
EDIT: Nevermind... Visual Studio's dialog editor is quite poor... it won't let you set the height of the combobox in the properties...

[Edited by - Ailus on July 13, 2007 10:38:17 PM]

##### Share on other sites
Ah, you've encountered the most evil "feature" of the windows common controls.

Have a look here:
http://blogs.msdn.com/oldnewthing/archive/2006/03/10/548537.aspx

I've always had to edit the .rc file manually to fix this. I'm not sure if there is a way to do it in the graphic dialog editor in MSVC... maybe someone else knows.

##### Share on other sites
There is:

In the dialog editor click on the drop down button of the combo box. The rect changes and you can set the height of the popup list.

##### Share on other sites
Yeah I just opened it in notepad++ and changed it myself. Its a pain to hit yes to the "reload this file" prompts every time, but oh well.

EDIT: Thanks Endurion. I never noticed that before. Its kind of difficult to tell because clicking it only activates the bottom "dragger-square" thing, which you then have to drag and imagine what the drop-down menu will look like because its clear. All the same though, its pretty easy now.

Thanks again everyone for the help!