Click here to Skip to main content
15,900,378 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all ,

I am trying to populate some data into the combobox control but when I run the program I do not see any data in it .
Here is the snippet :

C++
HWND hcombo  = CreateWindow(TEXT("COMBOBOX"), NULL, WS_VISIBLE | WS_CHILD  | WS_TABSTOP|CBS_DROPDOWNLIST|CBS_HASSTRINGS, 24, 8, 137, 25, hWnd, (HMENU) 1, NULL, NULL);

SendMessage(hcombo, CB_ADDSTRING, 0, (LPARAM) TEXT("Item1"));
SendMessage(hcombo, CB_ADDSTRING, 0, (LPARAM) TEXT("Item2"));
SendMessage(hcombo, CB_ADDSTRING, 0, (LPARAM) TEXT("Item3"));

Although if I set the cursel to some index as in SendMessage(hcombo, CB_SETCURSEL, 0, 0);
I do see the exact data in the combobox but the only data item rest do not populate in it .

I seek some help on it .
Many Thanks
Posted
Updated 20-Apr-19 11:56am
v2
Comments
Richard MacCutchan 24-Jan-15 5:07am    
I just tried your code and it runs fine. You need to check with your debugger to see what is going on.
iampradeepsharma 24-Jan-15 11:07am    
does it have to do with where the code was executed I mean I have put this code inside WM_CREATE inside switch case . Although I see on several other websites , they have placed it under WM_INITDIALOG . Although I tried to put it under it also but WM_INITDIALOG is never called . Do you have any insights on it .
Thanks Richard
Richard MacCutchan 24-Jan-15 11:30am    
It depends on your application. If it is a normal Window app then WM_CREATE is probably a good place. If it is a dialog then WM_INITDIALOG should be OK.

It may be that there is something else in your code that is causing the problem, but without seeing some more it is impossible to say what that might be. As I suggested previously, you should use your debugger to capture a bit more information.
iampradeepsharma 24-Jan-15 11:59am    
Do you actually see 3 items populated inside the combo box as with this code I tried on two machines but to no help .
Richard MacCutchan 24-Jan-15 12:03pm    
As I said before, "it runs fine", meaning it works correctly. So you must be doing something else in your code that corrupts, or otherwise destroys, something in your ComboBox.

1 solution

Based on this article[^], to make you code work, you need additional initialization as show in the example bellow:

int xpos = 100;            // Horizontal position of the window.
int ypos = 100;            // Vertical position of the window.
int nwidth = 200;          // Width of the window
int nheight = 200;         // Height of the window
HWND hwndParent = m_hwnd; // Handle to the parent window

HWND hWndComboBox = CreateWindow(WC_COMBOBOX, TEXT(""),
	CBS_DROPDOWN | CBS_HASSTRINGS | WS_CHILD | WS_OVERLAPPED | WS_VISIBLE,
	xpos, ypos, nwidth, nheight, hwndParent, NULL, HINST_THISCOMPONENT,
	NULL);

SendMessage(hcombo, CB_ADDSTRING, 0, (LPARAM)TEXT("Item1"));
SendMessage(hcombo, CB_ADDSTRING, 0, (LPARAM)TEXT("Item2"));
SendMessage(hcombo, CB_ADDSTRING, 0, (LPARAM)TEXT("Item3"));

// Send the CB_SETCURSEL message to display an initial item
//  in the selection field  
SendMessage(hWndComboBox, CB_SETCURSEL, (WPARAM)2, (LPARAM)0);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900