Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Comboboxes in WINAPI

Comboboxes in WINAPI

Scheduled Pinned Locked Moved C / C++ / MFC
questiondatabasebeta-testinghelpcode-review
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Ibana
    wrote on last edited by
    #1

    Hello there! I have a question regarding comboboxes in winapi. I have added a combobox to a dialog and some items in it. (look below in the code). But now I want to get feedback on the selected item in the combobox, but I don't now how do that. Lets say that I want a messagebox to come up when I select item with index 2. How would I do that? I guess that I should to it somewhere in WM_COMMAND but how..below is some of my code. Any help would be great! Regards /Peter LRESULT CALLBACK ToolboxProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam) { switch(Msg) { case WM_INITDIALOG: /*Add all the options to the combo box*/ char Buffer[64]; strcpy(Buffer,"Von Mises"); SendDlgItemMessage(hWndDlg,IDC_COMBO1,CB_INSERTSTRING,0,(LPARAM)Buffer); strcpy(Buffer,"Principal Major Z1"); SendDlgItemMessage(hWndDlg,IDC_COMBO1,CB_INSERTSTRING,1,(LPARAM)Buffer); strcpy(Buffer,"Principel Major Z2"); SendDlgItemMessage(hWndDlg,IDC_COMBO1,CB_INSERTSTRING,2,(LPARAM)Buffer); strcpy(Buffer,"Principel Minor Z1"); SendDlgItemMessage(hWndDlg,IDC_COMBO1,CB_INSERTSTRING,3,(LPARAM)Buffer); strcpy(Buffer,"Principel Minor Z2"); SendDlgItemMessage(hWndDlg,IDC_COMBO1,CB_INSERTSTRING,4,(LPARAM)Buffer); break; case WM_COMMAND: { switch(LOWORD(wParam)) { case IDC_COMBO1: { MessageBox(hWndDlg, "Combobox selected ", "Notice", MB_OK | MB_ICONINFORMATION); } break; } return 0; } break; } return FALSE; }

    T N 2 Replies Last reply
    0
    • I Ibana

      Hello there! I have a question regarding comboboxes in winapi. I have added a combobox to a dialog and some items in it. (look below in the code). But now I want to get feedback on the selected item in the combobox, but I don't now how do that. Lets say that I want a messagebox to come up when I select item with index 2. How would I do that? I guess that I should to it somewhere in WM_COMMAND but how..below is some of my code. Any help would be great! Regards /Peter LRESULT CALLBACK ToolboxProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam) { switch(Msg) { case WM_INITDIALOG: /*Add all the options to the combo box*/ char Buffer[64]; strcpy(Buffer,"Von Mises"); SendDlgItemMessage(hWndDlg,IDC_COMBO1,CB_INSERTSTRING,0,(LPARAM)Buffer); strcpy(Buffer,"Principal Major Z1"); SendDlgItemMessage(hWndDlg,IDC_COMBO1,CB_INSERTSTRING,1,(LPARAM)Buffer); strcpy(Buffer,"Principel Major Z2"); SendDlgItemMessage(hWndDlg,IDC_COMBO1,CB_INSERTSTRING,2,(LPARAM)Buffer); strcpy(Buffer,"Principel Minor Z1"); SendDlgItemMessage(hWndDlg,IDC_COMBO1,CB_INSERTSTRING,3,(LPARAM)Buffer); strcpy(Buffer,"Principel Minor Z2"); SendDlgItemMessage(hWndDlg,IDC_COMBO1,CB_INSERTSTRING,4,(LPARAM)Buffer); break; case WM_COMMAND: { switch(LOWORD(wParam)) { case IDC_COMBO1: { MessageBox(hWndDlg, "Combobox selected ", "Notice", MB_OK | MB_ICONINFORMATION); } break; } return 0; } break; } return FALSE; }

      T Offline
      T Offline
      Tom Wright
      wrote on last edited by
      #2

      You want to set an event on the CBN_SELCHANGE. When it returns to the OnCbnSelchangeServercombo function then check to see what was choosen and go from there. Hope this helps. Tom Wright tawright915@yahoo.com

      I 1 Reply Last reply
      0
      • T Tom Wright

        You want to set an event on the CBN_SELCHANGE. When it returns to the OnCbnSelchangeServercombo function then check to see what was choosen and go from there. Hope this helps. Tom Wright tawright915@yahoo.com

        I Offline
        I Offline
        Ibana
        wrote on last edited by
        #3

        Hi! thanks for your replay Tom. But Im not quite sure how to or where to implement that, Would be great with some code examples, cause I havent found much tutorials or examples on how to handle this. As you see in my first thread, I have given seperate indexes: *********** strcpy(Buffer,"Principal Minor Z2"); SendDlgItemMessage(hWndDlg,IDC_COMBO1,CB_INSERTSTRING,5,(LPARAM)Buffer); *************** I know that when option is selected the ON_CBN_SELENDOK message is sent, but how should I use it. So if someone could write me a code snip or recommened some tutorial I would be thankful, Best Regards /Peter

        T 1 Reply Last reply
        0
        • I Ibana

          Hello there! I have a question regarding comboboxes in winapi. I have added a combobox to a dialog and some items in it. (look below in the code). But now I want to get feedback on the selected item in the combobox, but I don't now how do that. Lets say that I want a messagebox to come up when I select item with index 2. How would I do that? I guess that I should to it somewhere in WM_COMMAND but how..below is some of my code. Any help would be great! Regards /Peter LRESULT CALLBACK ToolboxProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam) { switch(Msg) { case WM_INITDIALOG: /*Add all the options to the combo box*/ char Buffer[64]; strcpy(Buffer,"Von Mises"); SendDlgItemMessage(hWndDlg,IDC_COMBO1,CB_INSERTSTRING,0,(LPARAM)Buffer); strcpy(Buffer,"Principal Major Z1"); SendDlgItemMessage(hWndDlg,IDC_COMBO1,CB_INSERTSTRING,1,(LPARAM)Buffer); strcpy(Buffer,"Principel Major Z2"); SendDlgItemMessage(hWndDlg,IDC_COMBO1,CB_INSERTSTRING,2,(LPARAM)Buffer); strcpy(Buffer,"Principel Minor Z1"); SendDlgItemMessage(hWndDlg,IDC_COMBO1,CB_INSERTSTRING,3,(LPARAM)Buffer); strcpy(Buffer,"Principel Minor Z2"); SendDlgItemMessage(hWndDlg,IDC_COMBO1,CB_INSERTSTRING,4,(LPARAM)Buffer); break; case WM_COMMAND: { switch(LOWORD(wParam)) { case IDC_COMBO1: { MessageBox(hWndDlg, "Combobox selected ", "Notice", MB_OK | MB_ICONINFORMATION); } break; } return 0; } break; } return FALSE; }

          N Offline
          N Offline
          normanS
          wrote on last edited by
          #4

          I found an article in MSDN - in the "books" or "partial books", maybe? In my application I have a combo-box which allows selection of recording Primary or Secondary or both (this explains the names used in the code below!) In my main window procedure, I use simething like: case WM_COMMAND: switch(wParam & 0xFFFF) // Extract the low-order word { case ID_ToolbarRecordCombo: if (HIWORD(wParam) == CBN_CLOSEUP) { iComboBoxSelection = SendMessage(hWndRecordCombo, CB_GETCURSEL, 0, 0); // iComboBoxSelection values are an index based on // the order in which the strings are added in the // DrawToolbar function, i.e. Prim then Sec then Both. // If the selection has changed then change the source. if (iComboBoxSelection == 0 && recordingSelection != RecordPrimary) { recordingSelection = RecordPrimary; break; } else if (iComboBoxSelection == 1 && recordingSelection != RecordSecondary) { recordingSelection = RecordSecondary; break; } else if (iComboBoxSelection == 2 && recordingSelection != RecordBoth) { recordingSelection = RecordBoth; break; } // If the selection has not changed, do nothing! } else return(DefWindowProc(hwnd, message, wParam, lParam)); I hope this helps!

          I 1 Reply Last reply
          0
          • I Ibana

            Hi! thanks for your replay Tom. But Im not quite sure how to or where to implement that, Would be great with some code examples, cause I havent found much tutorials or examples on how to handle this. As you see in my first thread, I have given seperate indexes: *********** strcpy(Buffer,"Principal Minor Z2"); SendDlgItemMessage(hWndDlg,IDC_COMBO1,CB_INSERTSTRING,5,(LPARAM)Buffer); *************** I know that when option is selected the ON_CBN_SELENDOK message is sent, but how should I use it. So if someone could write me a code snip or recommened some tutorial I would be thankful, Best Regards /Peter

            T Offline
            T Offline
            Tom Wright
            wrote on last edited by
            #5

            Listen when it comes to using the studios...be ti .NET or VS6, I totally believe in using the wizards. There maybe times when they create code that does not work without some tweaking...but they work great 90% of the time. So here is how you do it. I'm not sure whether you are using the .NET studio or VS6 so I'm going to base this on the .NET studio. Go to you dialog screen. click on the combo box that you want to add the event to. Now right click on it and choose properties. Next click on the event button on the properties box toolbar (it's the one with the lightening bolt). Now choose the CBN_SELCHANGE and click the drop down arrow or make the function name whatever you want. This will create the function in your code. Then go to the code and do what ever you need to do when the user selects something from the combo box. Hope this helps. To see if this works put a message box in that function that run your program. Tom Tom Wright tawright915@yahoo.com

            I 1 Reply Last reply
            0
            • T Tom Wright

              Listen when it comes to using the studios...be ti .NET or VS6, I totally believe in using the wizards. There maybe times when they create code that does not work without some tweaking...but they work great 90% of the time. So here is how you do it. I'm not sure whether you are using the .NET studio or VS6 so I'm going to base this on the .NET studio. Go to you dialog screen. click on the combo box that you want to add the event to. Now right click on it and choose properties. Next click on the event button on the properties box toolbar (it's the one with the lightening bolt). Now choose the CBN_SELCHANGE and click the drop down arrow or make the function name whatever you want. This will create the function in your code. Then go to the code and do what ever you need to do when the user selects something from the combo box. Hope this helps. To see if this works put a message box in that function that run your program. Tom Tom Wright tawright915@yahoo.com

              I Offline
              I Offline
              Ibana
              wrote on last edited by
              #6

              Hello, Im using .Net and have done as you said, but when I click on the Lightning bulp(properties) nothing turns up,.. There is nothing to select. I created an application Win32 Console Applicaition and I havent added any support for MFC as default. Could this have anythinh to do with. I have selected Resources->Dialog any selected the dialog with my combobox but I cant select anything in the properties of the combobox.. /Peter

              1 Reply Last reply
              0
              • N normanS

                I found an article in MSDN - in the "books" or "partial books", maybe? In my application I have a combo-box which allows selection of recording Primary or Secondary or both (this explains the names used in the code below!) In my main window procedure, I use simething like: case WM_COMMAND: switch(wParam & 0xFFFF) // Extract the low-order word { case ID_ToolbarRecordCombo: if (HIWORD(wParam) == CBN_CLOSEUP) { iComboBoxSelection = SendMessage(hWndRecordCombo, CB_GETCURSEL, 0, 0); // iComboBoxSelection values are an index based on // the order in which the strings are added in the // DrawToolbar function, i.e. Prim then Sec then Both. // If the selection has changed then change the source. if (iComboBoxSelection == 0 && recordingSelection != RecordPrimary) { recordingSelection = RecordPrimary; break; } else if (iComboBoxSelection == 1 && recordingSelection != RecordSecondary) { recordingSelection = RecordSecondary; break; } else if (iComboBoxSelection == 2 && recordingSelection != RecordBoth) { recordingSelection = RecordBoth; break; } // If the selection has not changed, do nothing! } else return(DefWindowProc(hwnd, message, wParam, lParam)); I hope this helps!

                I Offline
                I Offline
                Ibana
                wrote on last edited by
                #7

                thanks for your replay Norman, it helped me very much but it only replys if iComboBoxSelection is =0.... Is there something wrong with my inserttion where I use CB_INSERTSTRING followed by indexing ... or do I use the COMMAND: case wrong? /Peter LRESULT CALLBACK ToolboxProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam) { LRESULT iComboBoxSelection; switch(Msg) { case WM_INITDIALOG: /*Add all the options to the combo box*/ char Buffer[64]; strcpy(Buffer,"Von Mises Z1"); SendDlgItemMessage(hWndDlg,IDC_COMBO1,CB_INSERTSTRING,0,(LPARAM)Buffer); strcpy(Buffer,"Von Mises Z2"); SendDlgItemMessage(hWndDlg,IDC_COMBO1,CB_INSERTSTRING,1,(LPARAM)Buffer); strcpy(Buffer,"Principal Major Z1"); SendDlgItemMessage(hWndDlg,IDC_COMBO1,CB_INSERTSTRING,2,(LPARAM)Buffer); strcpy(Buffer,"Principal Major Z2"); SendDlgItemMessage(hWndDlg,IDC_COMBO1,CB_INSERTSTRING,3,(LPARAM)Buffer); strcpy(Buffer,"Principal Minor Z1"); SendDlgItemMessage(hWndDlg,IDC_COMBO1,CB_INSERTSTRING,4,(LPARAM)Buffer); strcpy(Buffer,"Principal Minor Z2"); //GetCurSel(); //iComboBoxSelection=SendDlgItemMessage(hWndDlg,IDC_COMBO1,CB_SETCURSEL, 0, 0); // SendDlgItemMessage(hWndDlg,IDC_COMBO1,ON_CBN_SELENDOK ,0,(LPARAM)Buffer); //SendDlgItemMessage(hWndDlg,IDC_COMBO1,CB_GETCURSEL, 0, 0); break; case WM_COMMAND: switch(LOWORD(wParam)) { case IDC_COMBO1: { if (HIWORD(wParam) == CBN_CLOSEUP) { iComboBoxSelection = SendMessage(hWndDlg, CB_GETCURSEL, 0, 0); if (iComboBoxSelection == 2) { MessageBox(hWnd, "iComboBoxSelection == 2", "Error", MB_OK | MB_ICONINFORMATION); } if (iComboBoxSelection == 1)MessageBox(hWnd, "iComboBoxSelection == 1", "Error", MB_OK | MB_ICONINFORMATION); if (iComboBoxSelection == 0)MessageBox(hWnd, "iComboBoxSelection == 0", "Error", MB_OK | MB_ICONINFORMATION); } break; } } break; } return FALSE; }

                I 1 Reply Last reply
                0
                • I Ibana

                  thanks for your replay Norman, it helped me very much but it only replys if iComboBoxSelection is =0.... Is there something wrong with my inserttion where I use CB_INSERTSTRING followed by indexing ... or do I use the COMMAND: case wrong? /Peter LRESULT CALLBACK ToolboxProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam) { LRESULT iComboBoxSelection; switch(Msg) { case WM_INITDIALOG: /*Add all the options to the combo box*/ char Buffer[64]; strcpy(Buffer,"Von Mises Z1"); SendDlgItemMessage(hWndDlg,IDC_COMBO1,CB_INSERTSTRING,0,(LPARAM)Buffer); strcpy(Buffer,"Von Mises Z2"); SendDlgItemMessage(hWndDlg,IDC_COMBO1,CB_INSERTSTRING,1,(LPARAM)Buffer); strcpy(Buffer,"Principal Major Z1"); SendDlgItemMessage(hWndDlg,IDC_COMBO1,CB_INSERTSTRING,2,(LPARAM)Buffer); strcpy(Buffer,"Principal Major Z2"); SendDlgItemMessage(hWndDlg,IDC_COMBO1,CB_INSERTSTRING,3,(LPARAM)Buffer); strcpy(Buffer,"Principal Minor Z1"); SendDlgItemMessage(hWndDlg,IDC_COMBO1,CB_INSERTSTRING,4,(LPARAM)Buffer); strcpy(Buffer,"Principal Minor Z2"); //GetCurSel(); //iComboBoxSelection=SendDlgItemMessage(hWndDlg,IDC_COMBO1,CB_SETCURSEL, 0, 0); // SendDlgItemMessage(hWndDlg,IDC_COMBO1,ON_CBN_SELENDOK ,0,(LPARAM)Buffer); //SendDlgItemMessage(hWndDlg,IDC_COMBO1,CB_GETCURSEL, 0, 0); break; case WM_COMMAND: switch(LOWORD(wParam)) { case IDC_COMBO1: { if (HIWORD(wParam) == CBN_CLOSEUP) { iComboBoxSelection = SendMessage(hWndDlg, CB_GETCURSEL, 0, 0); if (iComboBoxSelection == 2) { MessageBox(hWnd, "iComboBoxSelection == 2", "Error", MB_OK | MB_ICONINFORMATION); } if (iComboBoxSelection == 1)MessageBox(hWnd, "iComboBoxSelection == 1", "Error", MB_OK | MB_ICONINFORMATION); if (iComboBoxSelection == 0)MessageBox(hWnd, "iComboBoxSelection == 0", "Error", MB_OK | MB_ICONINFORMATION); } break; } } break; } return FALSE; }

                  I Offline
                  I Offline
                  Ibana
                  wrote on last edited by
                  #8

                  I found the error , thanks for all your help guys,-- /Peter

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups