CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2013
    Posts
    34

    Combo Box selection.

    ok, I made my window, made a drop box, but how do I know what item was selected?

    with buttons it's easy, since there is only one parameter, but drop down menu's have multiple so I tried making an HMENU, but I can't find a way to link up the HMENU with the actual drop-box selections. here is the code snippits:

    (Window creation is left out for clutter reasons, I'm mainly focusing on the message handling)

    I define the two labels I want to choose from, and make a handle ID for my combo-box:

    Code:
    #define ID_Boo 0
    #define ID_Voo 1
    
    HWND hWndComboBox;

    then (Under the WM_CREATE message) I make the HMENU, Create the DropBox window, and fill the drop down box (I think this is the incorrect way to do this, but I'm not sure what is the right way.. hence my question)

    Code:
                    //create HMENU, and fill it
    		HMENU hFile = CreateMenu();
    		AppendMenu(hFile, MF_STRING, ID_Boo, L"Boo");
    		AppendMenu(hFile, MF_STRING, ID_Voo, L"voo");
    		
    
                    //create combobox, and get it to send messages through 'hFile'
    		hWndComboBox = CreateWindow(L"COMBOBOX", NULL,CBS_DROPDOWN  | WS_CHILD | CBS_HASSTRINGS | WS_OVERLAPPED | WS_VISIBLE, 20, 20, 100, 60, window, hFile, NULL, NULL);
    
    		// Add strings to combobox.
    		SendMessage(hWndComboBox,(UINT) CB_ADDSTRING,(WPARAM) 0,(LPARAM) L"Boo"); 
    		SendMessage(hWndComboBox,(UINT) CB_ADDSTRING,(WPARAM) 1,(LPARAM) L"voo"); 
    		//set the opening text to the first item in the list	
    		SendMessage(hWndComboBox, CB_SETCURSEL, (WPARAM)0, NULL);
    This works beautifully, nice drop down box, filled with the text I want. but i get issues when I try to get messages from it, this is how I tried:

    Code:
    case WM_COMMAND:
    
    switch(LOWORD(wp))
    {
            case ID_Boo:
    	{
    		cout << "yay!!";
                    break;
    	}
            case ID_Voo:
    	{
    		cout << "noo!!";
                    break;
    	}
    }
    break;
    note: I made the window under a console, so couts work fine

    it works fine, if instead of a combo box I make a SetMenu, but I don't Want a setmenu lol

    any thoughts? suggestions? opinions? need to see the whole code?

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Combo Box selection.


  3. #3
    Join Date
    Apr 2013
    Posts
    34

    Re: Combo Box selection.

    Quote Originally Posted by GCDEF View Post
    indeed, i just tried a menu since when I tried to add a normal include like you would with buttons, I got the error "not HMENU type"

    but anyways, I got it to work. here's what I did:

    Defined one name for the ComboBox, and made a handle for the combo box:

    Code:
    #define ID_Boo 0
    
    HWND hWndComboBox;
    under WM_CREATE I made the Combo box, and attached that combo box to 'ID_Boo', and add things in the combobox

    Code:
    //make the combobox
    hWndComboBox = CreateWindow(L"COMBOBOX", NULL,CBS_DROPDOWN  | WS_CHILD | CBS_HASSTRINGS | WS_OVERLAPPED | WS_VISIBLE, 20, 20, 100, 60, window, NULL, NULL, NULL);
    
    //attach combobox to 'ID_Boo'
    GetDlgItem(hWndComboBox, ID_Boo);
    
    //fill up the combobox
    SendMessage(hWndComboBox, CB_ADDSTRING, 0, (LPARAM) L"Boo");
    SendMessage(hWndComboBox, CB_ADDSTRING, 1, (LPARAM) L"voo");
    
    //set the default text to the first object in combo box (makes it look pretty :p)
    SendMessage(hWndComboBox, CB_SETCURSEL, (WPARAM)0, NULL);

    now, to get the messages from the combo box you do:

    Code:
    		case WM_COMMAND:
    			switch(LOWORD(wp))
    			{
    			case ID_Boo:
    				{					
    					pos = (int)SendMessage(hWndComboBox, CB_GETCURSEL, 0, 0); 
    					cout << pos << " ";
    				}
    			}
    			break;
    and I'll just use if statements to get rid of duplicates, and separate 'selection 1' from 'selection 2' etc.

    thanks though!

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Combo Box selection.

    //attach combobox to 'ID_Boo'
    GetDlgItem(hWndComboBox, ID_Boo)
    ;
    Whatever you think of it, this is not.

    You'd better get some book on Win32 API, like Petzold's Programming Windows.
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured