No data showing in combo box

This is a discussion on No data showing in combo box within the Windows Programming forums, part of the Platform Specific Boards category; I'm adding a combo box to a dialog box in the resource editor of Visual Studio and am running into ...

  1. #1
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728

    No data showing in combo box

    I'm adding a combo box to a dialog box in the resource editor of Visual Studio and am running into problems. The combo box is a drop list, and I'm adding data to the list via the properties menu. If I test the box while still in the resource editor (Ctrl-T) it works perfectly fine, I can see all of the items in the list and select them at will. But when I run a program that calls the dialog box, the list is suddenly empty. Heres the simple test program I've made to run the dialog box. Its just a blank window with one menu option with the ID of ID_TEST_TEST that calls the dialog box which contains the combo box and an OK button:
    Code:
    #include<windows.h>
    #include "resource.h"
    using namespace std;
    
    
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    BOOL CALLBACK MainDialogProc(HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
    {
    	HMENU hMenu;
    	static TCHAR szAppName[]=TEXT("Assessment Runner");
    	HWND hwnd;   // handle to a window
    	MSG msg;   // message
    	WNDCLASS wndclass;  // window class we are declaring
    
    
    	// Now we set the attributes for our window
    	wndclass.style=CS_HREDRAW | CS_VREDRAW;  // Style, in this case can be redrawn
    	wndclass.lpfnWndProc=WndProc;  // window process name - handles message calls
    	wndclass.cbClsExtra=0;  // Extra space for the window
    	wndclass.cbWndExtra=0;  // ditto
    	wndclass.hInstance=hInstance;  // program instance name passed to WinMain
    	wndclass.hIcon=LoadIcon(NULL, IDI_APPLICATION);  // Load icon for the tray
    	wndclass.hCursor=LoadCursor(NULL, IDC_ARROW);  // Load icon for the mouse arrow
    	wndclass.hbrBackground=(HBRUSH) GetStockObject(WHITE_BRUSH);  // Load background color
    	wndclass.lpszMenuName=NULL;  // Set menu - none here
    	wndclass.lpszClassName=szAppName;  // Sets window name, taken from above
    
    	hMenu=LoadMenu(hInstance,MAKEINTRESOURCE(IDR_MENU1));
    
    	if (!RegisterClass(&wndclass))
    	{
    		MessageBox(NULL, TEXT("Error running program!"), szAppName, MB_ICONERROR);
    		return 0;
    	}
    
    
    
    	hwnd=CreateWindow(szAppName, TEXT("Assessment Runer"), WS_OVERLAPPEDWINDOW,
    		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, hMenu,
    		hInstance, NULL);  // Create the window!  Name, Title, Style, x Position, y Position
    	                       // X size, Y size, parent window handle, window menu handle, 
    						   // program instance handle, creation parameters(passing variables)
    
    
    	ShowWindow(hwnd, iCmdShow);  // now show the window
    	UpdateWindow(hwnd);   // update it
    
    	while (GetMessage(&msg, NULL, 0, 0))  // gets message from mouse and keyboard, always non
    		                                  // zero until you quit the window
    	{
    		TranslateMessage(&msg);           // translates message from message queue (mouse/keyboard/etc)
    		DispatchMessage(&msg);			  // sends to WndProc to translate and tell what to do!
    	}
    
    	return msg.wParam;
    
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	static HINSTANCE hInstance;
    
    	switch (message)
    	{
    	case WM_CREATE:
    		hInstance=((LPCREATESTRUCT) lParam)->hInstance;
    		return 0;
    
    	case WM_COMMAND:
    		switch (LOWORD(wParam))
    		{
    		case ID_TEST_TEST:
    			DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), hwnd, MainDialogProc);
    			break;
    		}
    
    		return 0;
    
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		return 0;
    	}
    
    	return DefWindowProc(hwnd, message, wParam, lParam);
    }
    
    BOOL CALLBACK MainDialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
    {
    
    	switch (message)
    	{
    	case WM_INITDIALOG:
    		return TRUE;
    
    	case WM_COMMAND:
    		switch (LOWORD (wParam))
    		{
    		case IDOK:  // user clicked OK button, return
    			EndDialog(hDlg, TRUE);
    			return TRUE;
    		}
    		break;
    	}
    
    	return FALSE;
    }

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,643
    If you look at your resource script, you'll see that the combo box data is stored in a "DLGINIT" resource. This is used internally by CDialog to perform certain dialog initializations on WM_INITDIALOG.

    CDialog is MFC, so you have to use MFC to make it work. Otherwise, add the data manually on WM_INITDIALOG.

    gg

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Ok thanks, good to know. Its been over a year since I've done Windows programming and it feels like I've forgotten everything. I can't test this til tomorrow unfortunately but to add data manually do I use the SendMessage function? For example:
    Code:
    SendMessage(hwndCombo, LB_ADDSTRING , 0 , TEXT("Test data"));
    If this is correct, how do I get the handle for a combo box that is created using the resource editor as opposed to CreateWindow? If the ID for the combo box is ID_COMBO1 and the handle to the dialog box is hDlg I'm guessing:
    Code:
    hwndCombo=GetDlgItem(hDlg, IDC_COMBO1);
    Is this correct?

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,643
    Yes and yes, except use CB_ADDSTRING it it's really a combo box.

    gg

  5. #5
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Thanks, you da man

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    11
    OK, I'm doing the same thing as PJYelton, no data in combo box when I enter it via properties dialog. So, I make an empty combobox and the add data to it with SendMessage, but i doesn't work either. Here's some code:

    Code:
    case WM_INITDIALOG:
    	hwndcombo = GetDlgItem (hdlg, IDC_COMBO1) ;
    	if(hwndcombo  == NULL)
    		MessageBox(NULL, "Bad dialog handle", "Info", MB_OK);
    
    	res = SendMessage (hwndcombo, CB_ADDSTRING, 0, (LPARAM) TEXT("Data1")) ;
    	res = SendMessage (hwndcombo, CB_ADDSTRING, 1, (LPARAM) TEXT("Data2")) ;
    	res = SendMessage (hwndcombo, CB_ADDSTRING, 2, (LPARAM) TEXT("Data3")) ;
    	res = SendMessage (hwndcombo, CB_ADDSTRING, 3, (LPARAM) TEXT("Data4")) ;
    	//res = SendMessage (hwndcombo, CB_ADDSTRING, 4, (LPARAM) TEXT("Data5")) ;
    			
    	if(res != LB_OKAY)
    		MessageBox(NULL, "Error::SendMessage", "Info", MB_OK);
    			
    	return TRUE;
    Now, this messegebox does popup, meaning that 4th SendMessage failed. But if I comment out that 4th SendMessage it works fine, that is, first 3 SendMessage return LB_OKAY. In either case, there is no data in combo box. Help?
    Last edited by gargamel; 04-29-2005 at 06:27 PM.

  7. #7
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Welcome to the boards. If you haven't already done so then please take some time to familiarise yourself with the faq:
    http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

    Make sure you have a look at the the posting guidelines:
    http://cboard.cprogramming.com/annou...ouncementid=51
    Following the rules will ensure you get a prompt answer to your question.

    Remember, too, that the board has a search facility, a link is at the top of your screen:
    http://cboard.cprogramming.com/search.php
    It will often get you a quicker answer to your questions than waiting for a response to one you have posted.


    If you have any questions about this you may ask or you can contact one of our forum leaders:

    http://cboard.cprogramming.com/showgroups.php

    ***********

    What's LB_OKAY? The CB_ADDSTRING message specifies three possible return values, namely the zero-based index of the added string (success), CB_ERR (failure) or CB_ERRSPACE(failure).

    BTW, please don't bump old threads - please take the time to read through the information provided at the beginning of this post.
    Last edited by Ken Fitlike; 04-29-2005 at 07:22 PM.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reset Combo Control VS C++ 6.0
    By WaterNut in forum Windows Programming
    Replies: 8
    Last Post: 12-26-2007, 09:37 AM
  2. Dialog Box Problems
    By Morgul in forum Windows Programming
    Replies: 21
    Last Post: 05-31-2005, 05:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Replies: 2
    Last Post: 08-20-2004, 09:10 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21