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. Getting the Combo Box selected item using Win32 API.

Getting the Combo Box selected item using Win32 API.

Scheduled Pinned Locked Moved C / C++ / MFC
questionjsonhelp
21 Posts 4 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.
  • S Offline
    S Offline
    Suresh H
    wrote on last edited by
    #1

    Hello All, I am adding values 0,1,2 to a combo box in Initdialog switch(Message) { case WM_INITDIALOG: { HWND hwndCombo = GetDlgItem(hwnd, IDC_CB); SendMessage(hwndCombo , CB_ADDSTRING,0, (LPARAM) "0"); SendMessage(hwndCombo , CB_ADDSTRING,0, (LPARAM) "1"); SendMessage(hwndCombo , CB_ADDSTRING,0, (LPARAM) "2"); } in other function I want to use the value of the combo box, (user selected item) for that I am using below code int F; HWND HCBF = GetDlgItem(hwnd, IDC_CB); SendMessage(HCBF , CB_GETITEMDATA,(WPARAM)F, 0); This code has no error but I am getting some junk value and i am not geting the values 0,1,2 Can u please tell me what is the error in the above code ??? Thanking you, Suresh HC.

    P 1 Reply Last reply
    0
    • S Suresh H

      Hello All, I am adding values 0,1,2 to a combo box in Initdialog switch(Message) { case WM_INITDIALOG: { HWND hwndCombo = GetDlgItem(hwnd, IDC_CB); SendMessage(hwndCombo , CB_ADDSTRING,0, (LPARAM) "0"); SendMessage(hwndCombo , CB_ADDSTRING,0, (LPARAM) "1"); SendMessage(hwndCombo , CB_ADDSTRING,0, (LPARAM) "2"); } in other function I want to use the value of the combo box, (user selected item) for that I am using below code int F; HWND HCBF = GetDlgItem(hwnd, IDC_CB); SendMessage(HCBF , CB_GETITEMDATA,(WPARAM)F, 0); This code has no error but I am getting some junk value and i am not geting the values 0,1,2 Can u please tell me what is the error in the above code ??? Thanking you, Suresh HC.

      P Offline
      P Offline
      prasad_som
      wrote on last edited by
      #2

      Suresh H wrote:

      int F; HWND HCBF = GetDlgItem(hwnd, IDC_CB); SendMessage(HCBF , CB_GETITEMDATA,(WPARAM)F, 0);

      Here , you have not initialized F. Passing junk values as index. Pass index you want there. CB_GETITEMDATA is not right message to send in this case, too. You need to use CB_GETLBTEXT.

      Prasad Notifier using ATL | Operator new[],delete[][^]

      S 1 Reply Last reply
      0
      • P prasad_som

        Suresh H wrote:

        int F; HWND HCBF = GetDlgItem(hwnd, IDC_CB); SendMessage(HCBF , CB_GETITEMDATA,(WPARAM)F, 0);

        Here , you have not initialized F. Passing junk values as index. Pass index you want there. CB_GETITEMDATA is not right message to send in this case, too. You need to use CB_GETLBTEXT.

        Prasad Notifier using ATL | Operator new[],delete[][^]

        S Offline
        S Offline
        Suresh H
        wrote on last edited by
        #3

        Hi Prasad, Thanks for the response. Actually I want to extract the value of the Combo Box (user selected value) In combo box we have values from 0,1,2,3 ---- so on when user selects some value fro list any number , I want to extract that number and I want to use that number in the program. Can u please tell me what I have to do ??

        H P 2 Replies Last reply
        0
        • S Suresh H

          Hi Prasad, Thanks for the response. Actually I want to extract the value of the Combo Box (user selected value) In combo box we have values from 0,1,2,3 ---- so on when user selects some value fro list any number , I want to extract that number and I want to use that number in the program. Can u please tell me what I have to do ??

          H Offline
          H Offline
          Hamid Taebi
          wrote on last edited by
          #4

          You must use of CB_GETCURSEL for get index to currently selected item instead use of 0


          WhiteSky


          1 Reply Last reply
          0
          • S Suresh H

            Hi Prasad, Thanks for the response. Actually I want to extract the value of the Combo Box (user selected value) In combo box we have values from 0,1,2,3 ---- so on when user selects some value fro list any number , I want to extract that number and I want to use that number in the program. Can u please tell me what I have to do ??

            P Offline
            P Offline
            prasad_som
            wrote on last edited by
            #5

            This code should suffice,

            int nIndx =::SendMessage(hCombo,CB_GETCURSEL,0,0);
            TCHAR buff[100];
            ::SendMessage(h,CB_GETLBTEXT,(WPARAM)nIndx,(LPARAM)buff);
            //buff will contain text returned

            Prasad Notifier using ATL | Operator new[],delete[][^]

            H S J 3 Replies Last reply
            0
            • P prasad_som

              This code should suffice,

              int nIndx =::SendMessage(hCombo,CB_GETCURSEL,0,0);
              TCHAR buff[100];
              ::SendMessage(h,CB_GETLBTEXT,(WPARAM)nIndx,(LPARAM)buff);
              //buff will contain text returned

              Prasad Notifier using ATL | Operator new[],delete[][^]

              H Offline
              H Offline
              Hamid Taebi
              wrote on last edited by
              #6

              And he needs to a convert atoi ;)


              WhiteSky


              P 1 Reply Last reply
              0
              • H Hamid Taebi

                And he needs to a convert atoi ;)


                WhiteSky


                P Offline
                P Offline
                prasad_som
                wrote on last edited by
                #7

                WhiteSky wrote:

                And he needs to a convert atoi

                Why ?

                Prasad Notifier using ATL | Operator new[],delete[][^]

                H 1 Reply Last reply
                0
                • P prasad_som

                  This code should suffice,

                  int nIndx =::SendMessage(hCombo,CB_GETCURSEL,0,0);
                  TCHAR buff[100];
                  ::SendMessage(h,CB_GETLBTEXT,(WPARAM)nIndx,(LPARAM)buff);
                  //buff will contain text returned

                  Prasad Notifier using ATL | Operator new[],delete[][^]

                  S Offline
                  S Offline
                  Suresh H
                  wrote on last edited by
                  #8

                  Thanks Prasad Code is working. Thanks for taking some time for me and helping me. In yr code 1st line was enough for me int nIndx =::SendMessage(hCombo,CB_GETCURSEL,0,0); since in the combo box I have values from 0,1,2,3,4,5 and so on … so as I get the Index number value will be the same as the CB content. I did not know about the CB index. Thank you very much.:)

                  P 1 Reply Last reply
                  0
                  • P prasad_som

                    WhiteSky wrote:

                    And he needs to a convert atoi

                    Why ?

                    Prasad Notifier using ATL | Operator new[],delete[][^]

                    H Offline
                    H Offline
                    Hamid Taebi
                    wrote on last edited by
                    #9

                    If he wants to use of numeric values


                    WhiteSky


                    S P 2 Replies Last reply
                    0
                    • H Hamid Taebi

                      If he wants to use of numeric values


                      WhiteSky


                      S Offline
                      S Offline
                      Suresh H
                      wrote on last edited by
                      #10

                      Thanks WhiteSky for the Response, yr info is also very use full for me. I will keep a note of that.

                      H 1 Reply Last reply
                      0
                      • H Hamid Taebi

                        If he wants to use of numeric values


                        WhiteSky


                        P Offline
                        P Offline
                        prasad_som
                        wrote on last edited by
                        #11

                        Ok, you was refering to extracting numbers.

                        Prasad Notifier using ATL | Operator new[],delete[][^]

                        H 1 Reply Last reply
                        0
                        • S Suresh H

                          Thanks Prasad Code is working. Thanks for taking some time for me and helping me. In yr code 1st line was enough for me int nIndx =::SendMessage(hCombo,CB_GETCURSEL,0,0); since in the combo box I have values from 0,1,2,3,4,5 and so on … so as I get the Index number value will be the same as the CB content. I did not know about the CB index. Thank you very much.:)

                          P Offline
                          P Offline
                          prasad_som
                          wrote on last edited by
                          #12

                          Suresh H wrote:

                          since in the combo box I have values from 0,1,2,3,4,5 and so on … so as I get the Index number value will be the same as the CB content.

                          Seems very strange logic. But, use CB_GETLBTEXT message, as it will not be the case always.

                          Prasad Notifier using ATL | Operator new[],delete[][^]

                          S 1 Reply Last reply
                          0
                          • P prasad_som

                            Ok, you was refering to extracting numbers.

                            Prasad Notifier using ATL | Operator new[],delete[][^]

                            H Offline
                            H Offline
                            Hamid Taebi
                            wrote on last edited by
                            #13

                            And also one other thing if he wants to use CB_GETCURSEL(return values) for works on his programs(not for combobox)its not good idea because it returns -1,0,1,...but if he has values like 100,200,300 on his combobox... return values of CB_GETCURSEL not helpfuls ;)


                            WhiteSky


                            P 1 Reply Last reply
                            0
                            • P prasad_som

                              Suresh H wrote:

                              since in the combo box I have values from 0,1,2,3,4,5 and so on … so as I get the Index number value will be the same as the CB content.

                              Seems very strange logic. But, use CB_GETLBTEXT message, as it will not be the case always.

                              Prasad Notifier using ATL | Operator new[],delete[][^]

                              S Offline
                              S Offline
                              Suresh H
                              wrote on last edited by
                              #14

                              Yes Prasad I will make use of it.

                              1 Reply Last reply
                              0
                              • S Suresh H

                                Thanks WhiteSky for the Response, yr info is also very use full for me. I will keep a note of that.

                                H Offline
                                H Offline
                                Hamid Taebi
                                wrote on last edited by
                                #15

                                Thank you ;)


                                WhiteSky


                                1 Reply Last reply
                                0
                                • H Hamid Taebi

                                  And also one other thing if he wants to use CB_GETCURSEL(return values) for works on his programs(not for combobox)its not good idea because it returns -1,0,1,...but if he has values like 100,200,300 on his combobox... return values of CB_GETCURSEL not helpfuls ;)


                                  WhiteSky


                                  P Offline
                                  P Offline
                                  prasad_som
                                  wrote on last edited by
                                  #16

                                  WhiteSky wrote:

                                  CB_GETCURSEL(return values) for works on his programs(not for combobox)

                                  :confused:

                                  WhiteSky wrote:

                                  its not good idea because it returns -1,0,1,...but if he has values like 100,200,300 on his combobox... return values of CB_GETCURSEL not helpfuls

                                  I'm really not getting , what you are trying to say?

                                  Prasad Notifier using ATL | Operator new[],delete[][^]

                                  H 1 Reply Last reply
                                  0
                                  • P prasad_som

                                    WhiteSky wrote:

                                    CB_GETCURSEL(return values) for works on his programs(not for combobox)

                                    :confused:

                                    WhiteSky wrote:

                                    its not good idea because it returns -1,0,1,...but if he has values like 100,200,300 on his combobox... return values of CB_GETCURSEL not helpfuls

                                    I'm really not getting , what you are trying to say?

                                    Prasad Notifier using ATL | Operator new[],delete[][^]

                                    H Offline
                                    H Offline
                                    Hamid Taebi
                                    wrote on last edited by
                                    #17

                                    I said its better he uses of value on combobox ;)


                                    WhiteSky


                                    P 1 Reply Last reply
                                    0
                                    • H Hamid Taebi

                                      I said its better he uses of value on combobox ;)


                                      WhiteSky


                                      P Offline
                                      P Offline
                                      prasad_som
                                      wrote on last edited by
                                      #18

                                      Haven't you seen this[^]?

                                      Prasad Notifier using ATL | Operator new[],delete[][^]

                                      1 Reply Last reply
                                      0
                                      • P prasad_som

                                        This code should suffice,

                                        int nIndx =::SendMessage(hCombo,CB_GETCURSEL,0,0);
                                        TCHAR buff[100];
                                        ::SendMessage(h,CB_GETLBTEXT,(WPARAM)nIndx,(LPARAM)buff);
                                        //buff will contain text returned

                                        Prasad Notifier using ATL | Operator new[],delete[][^]

                                        J Offline
                                        J Offline
                                        jhwurmbach
                                        wrote on last edited by
                                        #19

                                        For CB_GETLBTEXT, MSDn says: lParam Pointer to the buffer that receives the string. The buffer must have sufficient space for the string and a terminating null character. With your 100 TCHARS you might or might not be ok. MSDN gives the solution in the next sentence: You can send a CB_GETLBTEXTLEN message prior to the CB_GETLBTEXT message to retrieve the length, in TCHARs, of the string. So, the code-snippet would better be:

                                        int nIndx =::SendMessage(hCombo,CB_GETCURSEL,0,0);
                                        const LRESULT nSize = ::SendMessage(hCombo, CB_GETLBTEXTLEN, (WPARAM)nIndx, 0);
                                        TCHAR buff[nSize+1];
                                        ::SendMessage(h,CB_GETLBTEXT,(WPARAM)nIndx,(LPARAM)buff);
                                        //buff will contain text returned


                                        "We trained hard, but it seemed that every time we were beginning to form up into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising: and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation." -- Caius Petronius, Roman Consul, 66 A.D.

                                        P 1 Reply Last reply
                                        0
                                        • J jhwurmbach

                                          For CB_GETLBTEXT, MSDn says: lParam Pointer to the buffer that receives the string. The buffer must have sufficient space for the string and a terminating null character. With your 100 TCHARS you might or might not be ok. MSDN gives the solution in the next sentence: You can send a CB_GETLBTEXTLEN message prior to the CB_GETLBTEXT message to retrieve the length, in TCHARs, of the string. So, the code-snippet would better be:

                                          int nIndx =::SendMessage(hCombo,CB_GETCURSEL,0,0);
                                          const LRESULT nSize = ::SendMessage(hCombo, CB_GETLBTEXTLEN, (WPARAM)nIndx, 0);
                                          TCHAR buff[nSize+1];
                                          ::SendMessage(h,CB_GETLBTEXT,(WPARAM)nIndx,(LPARAM)buff);
                                          //buff will contain text returned


                                          "We trained hard, but it seemed that every time we were beginning to form up into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising: and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation." -- Caius Petronius, Roman Consul, 66 A.D.

                                          P Offline
                                          P Offline
                                          prasad_som
                                          wrote on last edited by
                                          #20

                                          jhwurmbach wrote:

                                          lParam Pointer to the buffer that receives the string. The buffer must have sufficient space for the string and a terminating null character. With your 100 TCHARS you might or might not be ok.

                                          True, It is just a quick fix.

                                          jhwurmbach wrote:

                                          TCHAR buff[nSize+1];::SendMessage(h,CB_GETLBTEXT,(WPARAM)nIndx,(LPARAM)buff);//buff will contain text returned

                                          Again, small correction here, need to allocate memory on heap.

                                           TCHAR \*buff = new TCHAR\[nIndx+1\];
                                          ::SendMessage(hCombo,CB\_GETLBTEXT,(WPARAM)0,(LPARAM)buff);
                                          //use delete \[\] buff; after use
                                          

                                          Prasad Notifier using ATL | Operator new[],delete[][^]

                                          J 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