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. Deleting an item from list box using API

Deleting an item from list box using API

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

    Hello all, I have list box IDC_FILE_LIST and a add and remove (IDC_REMOVE) button. After adding some items in the list box, I want to remove an item which is selected by the user on clicking on remove button. For that I have used this code and its not working item selected by the user is not getting deleted can please tell me what is the error in the below code. BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) { switch(Message) { case WM_COMMAND: switch(LOWORD(wParam)) { case IDC_REMOVE: { HWND hList = GetDlgItem(hwnd, IDC_FILE_LIST); int count = SendMessage(hList, LB_GETCOUNT, 0, 0); SendMessage(hwnd,(UINT) LB_DELETESTRING,count,lParam); } break; ----------- ---------------------- Tanking you, Suresh HC.

    M C 2 Replies Last reply
    0
    • S Suresh H

      Hello all, I have list box IDC_FILE_LIST and a add and remove (IDC_REMOVE) button. After adding some items in the list box, I want to remove an item which is selected by the user on clicking on remove button. For that I have used this code and its not working item selected by the user is not getting deleted can please tell me what is the error in the below code. BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) { switch(Message) { case WM_COMMAND: switch(LOWORD(wParam)) { case IDC_REMOVE: { HWND hList = GetDlgItem(hwnd, IDC_FILE_LIST); int count = SendMessage(hList, LB_GETCOUNT, 0, 0); SendMessage(hwnd,(UINT) LB_DELETESTRING,count,lParam); } break; ----------- ---------------------- Tanking you, Suresh HC.

      M Offline
      M Offline
      Mila025
      wrote on last edited by
      #2

      Hi, To get selected item you should use LB_GETCURSEL (LB_GETCOUNT returns number of items). Besides if you want to use LB_DELETESTRING you need first retrieve what text is at selected item - LB_GETTEXT.

      ----------- Mila

      S 1 Reply Last reply
      0
      • S Suresh H

        Hello all, I have list box IDC_FILE_LIST and a add and remove (IDC_REMOVE) button. After adding some items in the list box, I want to remove an item which is selected by the user on clicking on remove button. For that I have used this code and its not working item selected by the user is not getting deleted can please tell me what is the error in the below code. BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) { switch(Message) { case WM_COMMAND: switch(LOWORD(wParam)) { case IDC_REMOVE: { HWND hList = GetDlgItem(hwnd, IDC_FILE_LIST); int count = SendMessage(hList, LB_GETCOUNT, 0, 0); SendMessage(hwnd,(UINT) LB_DELETESTRING,count,lParam); } break; ----------- ---------------------- Tanking you, Suresh HC.

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #3

        The problem arises in the code below:

        Suresh H wrote:

        int count = SendMessage(hList, LB_GETCOUNT, 0, 0); SendMessage(hwnd,(UINT) LB_DELETESTRING,count,lParam);

        you are asking for the number of list items instead of the item currently selected. Moreover the lParam in the LB_DELETESTRING should be zero. Try this:

        int curSel = SendMessage(hList, LB_GETCURSEL, 0, 0);
        SendMessage(hwnd, (UINT) LB_DELETESTRING, curSel, (LPARAM)0);

        hope that helps

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

        S 1 Reply Last reply
        0
        • C CPallini

          The problem arises in the code below:

          Suresh H wrote:

          int count = SendMessage(hList, LB_GETCOUNT, 0, 0); SendMessage(hwnd,(UINT) LB_DELETESTRING,count,lParam);

          you are asking for the number of list items instead of the item currently selected. Moreover the lParam in the LB_DELETESTRING should be zero. Try this:

          int curSel = SendMessage(hList, LB_GETCURSEL, 0, 0);
          SendMessage(hwnd, (UINT) LB_DELETESTRING, curSel, (LPARAM)0);

          hope that helps

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

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

          Thank you very much sir the solution is working. int CurSel = SendMessage(GetDlgItem(hwnd, IDC_FILE_LIST), LB_GETCURSEL, 0, 0); SendMessage(GetDlgItem(hwnd, IDC_FILE_LIST), LB_DELETESTRING, CurSel, 0);

          C 1 Reply Last reply
          0
          • M Mila025

            Hi, To get selected item you should use LB_GETCURSEL (LB_GETCOUNT returns number of items). Besides if you want to use LB_DELETESTRING you need first retrieve what text is at selected item - LB_GETTEXT.

            ----------- Mila

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

            Thank you very much Mila for your response. Your comments helped me a lot.

            1 Reply Last reply
            0
            • S Suresh H

              Thank you very much sir the solution is working. int CurSel = SendMessage(GetDlgItem(hwnd, IDC_FILE_LIST), LB_GETCURSEL, 0, 0); SendMessage(GetDlgItem(hwnd, IDC_FILE_LIST), LB_DELETESTRING, CurSel, 0);

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #6

              you are welcome! :):-D:)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

              S 1 Reply Last reply
              0
              • C CPallini

                you are welcome! :):-D:)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

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

                :):-D:)

                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