Adding Items (Strings) to Combo Box?

Started by
9 comments, last by Ailus 16 years, 9 months ago
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:
		{
            LoadSettingsBox(hDlg);
			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);
	//resBox->AddString((LPCSTR)"Test");
	HWND hRes = GetDlgItem(hDlg, IDC_RESOLUTION);
	SendMessage(hDlg, CB_INSERTSTRING,  0, (LPARAM)"640x480");

	
	//SendDlgItemMessage(hDlg, IDC_RESOLUTION, WM_SETTEXT,  0, (LPARAM)"640x480");
	/*SendDlgItemMessage(hDlg, IDC_RESOLUTION, CB_ADDSTRING, "800x600", 0);
	SendDlgItemMessage(hDlg, IDC_RESOLUTION, CB_ADDSTRING, "1024x768", 0);
	SendDlgItemMessage(hDlg, IDC_RESOLUTION, CB_ADDSTRING, "1152x864", 0);
	SendDlgItemMessage(hDlg, IDC_RESOLUTION, CB_ADDSTRING, "1280x720", 0);
	SendDlgItemMessage(hDlg, IDC_RESOLUTION, CB_ADDSTRING, "1280x1024", 0);
	SendDlgItemMessage(hDlg, IDC_RESOLUTION, CB_ADDSTRING, "1360x768", 0);
	SendDlgItemMessage(hDlg, IDC_RESOLUTION, CB_ADDSTRING, "1360x1024", 0);
	SendDlgItemMessage(hDlg, IDC_RESOLUTION, CB_ADDSTRING, "1440x900", 0);
	SendDlgItemMessage(hDlg, IDC_RESOLUTION, CB_ADDSTRING, "1600x1200", 0);
	SendDlgItemMessage(hDlg, IDC_RESOLUTION, CB_ADDSTRING, "1680x1050", 0);
	SendDlgItemMessage(hDlg, IDC_RESOLUTION, CB_ADDSTRING, "1792x1344", 0);
	SendDlgItemMessage(hDlg, IDC_RESOLUTION, CB_ADDSTRING, "1800x1440", 0);
	SendDlgItemMessage(hDlg, IDC_RESOLUTION, CB_ADDSTRING, "1856x1392", 0);
	SendDlgItemMessage(hDlg, IDC_RESOLUTION, CB_ADDSTRING, "1920x1080", 0);
	SendDlgItemMessage(hDlg, IDC_RESOLUTION, CB_ADDSTRING, "1920x1200", 0);
	SendDlgItemMessage(hDlg, IDC_RESOLUTION, CB_ADDSTRING, "1920x1440", 0);
	SendDlgItemMessage(hDlg, IDC_RESOLUTION, CB_ADDSTRING, "2048x1536", 0);*/
}
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...
Advertisement
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

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...
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.

How about this instead:
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).
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?
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...
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!
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]
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.
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.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

This topic is closed to new replies.

Advertisement