Adding Items to Combo Boxes via WIN API

I successfully created a combo box using the CreateWindow function. However, I can't figure out how to add items to it. After searching the net, this is what I found, but it doesn't work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;

	switch (message)
	{
	case WM_CREATE:
		CreateObjects(hWnd);
		break;
	case WM_COMMAND:
		. . .
                                . . .


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void CreateObjects(HWND hWnd)
{
	HWND OptBt1, OptBt2;

	//Option buttons
	OptBt1 = CreateWindow(TEXT("button"), TEXT("Backup"), WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON, 24, 8, 137, 25, hWnd, (HMENU) 1, NULL, NULL);
	OptBt2 = CreateWindow(TEXT("button"), TEXT("Restore"), WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON, 24, 40, 97, 25, hWnd, (HMENU) 2, NULL, NULL);
	SendMessage(OptBt1, BM_SETCHECK, BST_CHECKED, 0); //Checks OptBt1 on, so that it's selected by default
         . . .
         . . .
         cmb_box1 = CreateWindow(TEXT("combobox"), NULL, WS_VISIBLE | WS_CHILD  | WS_TABSTOP, 128, 72, 81, 21, frm4, (HMENU) 12, NULL, NULL);

const TCHAR* ComboBoxItems[] = { _T( "Item1" ), _T( "Item2" ), _T( "Item3" ) };
SendMessage(cmb_box1, CB_ADDSTRING, 0, (LPARAM) ComboBoxItems[0]);




The item "item 1" should appear in the box, but it doesn't. Any help is greatly appreciated.

DominicanJB
Last edited on
I assume your combobox is actually being created.
If so - then the height of the combobox is too small.
You have a height of 21.
Try a height of 210


You will probably find that only 1 item is visible Item1 - because you
cannot send an array of items to a combobox - you have to send each item individually.


Example Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void CreateObjects(HWND hWnd)
{
	HWND OptBt1, OptBt2;

	//Option buttons
	OptBt1 = CreateWindow(TEXT("button"), TEXT("Backup"), WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON, 24, 8, 137, 25, hWnd, (HMENU) 1, NULL, NULL);
	OptBt2 = CreateWindow(TEXT("button"), TEXT("Restore"), WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON, 24, 40, 97, 25, hWnd, (HMENU) 2, NULL, NULL);
	SendMessage(OptBt1, BM_SETCHECK, BST_CHECKED, 0); //Checks OptBt1 on, so that it's selected by default

    //slight change of combobox style - change height (and also add DBS_DROPDOWN style for better look)
   cmb_box1 = CreateWindow(TEXT("combobox"), NULL, WS_VISIBLE | WS_CHILD  | CBS_DROPDOWN|WS_TABSTOP, 128, 72, 81, 210, frm4, (HMENU) 12, NULL, NULL);

//const TCHAR* ComboBoxItems[] = { _T( "Item1" ), _T( "Item2" ), _T( "Item3" ) };

//Send combobox items individually
SendMessage(cmb_box1, CB_ADDSTRING, 0, (LPARAM) _T("Item1"));
SendMessage(cmb_box1, CB_ADDSTRING, 0, (LPARAM) _T("Item2"));
SendMessage(cmb_box1, CB_ADDSTRING, 0, (LPARAM) _T("Item3"));

SendMessage(cmb_box1, CB_SETCURSEL, 0, 0); //highlight/select the first option

}

guestgulkan,

You are right on the money. The height of the window was too small for the text to appear. I know I have to add each item individually. I was just trying to to see if I could just add the first item as a test. I have not tried it, but I'd assume a loop should also work like the following:
1
2
3
4
5
for(int a = 0; a < nItems; a++)
{
       SendMessage(cmb_box1, CB_ADDSTRING, 0, (LPARAM) ComboBoxItems[a]);

}


Thanks a bunch!
DominicanJB
Last edited on
Topic archived. No new replies allowed.