How to sync ListView checkbox with selection?(WIN32)











up vote
2
down vote

favorite












I'm writing a simple Win32 program that has a ListView with check box and multi-row selection enabled. :
enter image description here



It seems like checkbox checks and row selections are two distinct behaviors. Is there a way to sync these two behaviors, that whenever a check box is checked that row will be selected and whenever a row selected the corresponding checkbox will be check?










share|improve this question




























    up vote
    2
    down vote

    favorite












    I'm writing a simple Win32 program that has a ListView with check box and multi-row selection enabled. :
    enter image description here



    It seems like checkbox checks and row selections are two distinct behaviors. Is there a way to sync these two behaviors, that whenever a check box is checked that row will be selected and whenever a row selected the corresponding checkbox will be check?










    share|improve this question


























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I'm writing a simple Win32 program that has a ListView with check box and multi-row selection enabled. :
      enter image description here



      It seems like checkbox checks and row selections are two distinct behaviors. Is there a way to sync these two behaviors, that whenever a check box is checked that row will be selected and whenever a row selected the corresponding checkbox will be check?










      share|improve this question















      I'm writing a simple Win32 program that has a ListView with check box and multi-row selection enabled. :
      enter image description here



      It seems like checkbox checks and row selections are two distinct behaviors. Is there a way to sync these two behaviors, that whenever a check box is checked that row will be selected and whenever a row selected the corresponding checkbox will be check?







      c windows listview winapi win32gui






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 21 '16 at 4:52









      Barmak Shemirani

      20.3k42044




      20.3k42044










      asked May 20 '16 at 21:21









      Kelvin Zhang

      380210




      380210
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          "whenever a row is selected, the corresponding checkbox will be checked"



          Check WM_NOTIFY and LVIS_SELECTED flag to detect when user selects a row. And use ListView_SetCheckState to check the check-box:



          BOOL CALLBACK DialogProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
          {
          static HWND hListView;
          switch (msg)
          {
          case WM_INITDIALOG:
          hListView = GetDlgItem(hWnd, IDC_LIST1);
          break;

          case WM_NOTIFY:
          {
          NMHDR* header = (NMHDR*)lParam;
          NMLISTVIEW* nmlist = (NMLISTVIEW*)lParam;
          if (header && header->idFrom == IDC_LIST1 && header->code == LVN_ITEMCHANGED)
          if (nmlist->uNewState & LVIS_SELECTED)
          ListView_SetCheckState(hListView, nmlist->iItem, 1);
          break;
          }
          ...
          }


          "whenever a check box is checked that row will be selected"



          Check WM_NOTIFY and LVIS_STATEIMAGEMASK flag to detect when checkbox is checked, then use ListView_SetItemState to select the row.



          Also this can lead to recursive calls, because we change the row in response to checkbox, and we change the checkbox in response to row selection. Use the busy variable to stop recursive calls.



          case WM_NOTIFY:
          if (lParam)
          {
          NMHDR* header = (NMHDR*)lParam;
          NMLISTVIEW* nmlist = (NMLISTVIEW*)lParam;

          //use `busy` as a flag to prevent recursive calls:
          static BOOL busy = FALSE;
          if (!busy && header->hwndFrom == hListView && header->code == LVN_ITEMCHANGED)
          {
          busy = TRUE;
          if (nmlist->uNewState & LVIS_SELECTED)
          {
          //row has been selected => check the checkbox
          ListView_SetCheckState(hListView, nmlist->iItem, 1);
          }
          else if (nmlist->uNewState & LVIS_STATEIMAGEMASK)
          {
          //checkbox has been changed => select/unselect the row
          BOOL checked = ListView_GetCheckState(hListView, nmlist->iItem);
          ListView_SetItemState(hListView, nmlist->iItem,
          checked ? LVIS_SELECTED : 0, LVIS_SELECTED);
          }
          busy = FALSE;
          }
          }
          break;





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37356042%2fhow-to-sync-listview-checkbox-with-selectionwin32%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            "whenever a row is selected, the corresponding checkbox will be checked"



            Check WM_NOTIFY and LVIS_SELECTED flag to detect when user selects a row. And use ListView_SetCheckState to check the check-box:



            BOOL CALLBACK DialogProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
            {
            static HWND hListView;
            switch (msg)
            {
            case WM_INITDIALOG:
            hListView = GetDlgItem(hWnd, IDC_LIST1);
            break;

            case WM_NOTIFY:
            {
            NMHDR* header = (NMHDR*)lParam;
            NMLISTVIEW* nmlist = (NMLISTVIEW*)lParam;
            if (header && header->idFrom == IDC_LIST1 && header->code == LVN_ITEMCHANGED)
            if (nmlist->uNewState & LVIS_SELECTED)
            ListView_SetCheckState(hListView, nmlist->iItem, 1);
            break;
            }
            ...
            }


            "whenever a check box is checked that row will be selected"



            Check WM_NOTIFY and LVIS_STATEIMAGEMASK flag to detect when checkbox is checked, then use ListView_SetItemState to select the row.



            Also this can lead to recursive calls, because we change the row in response to checkbox, and we change the checkbox in response to row selection. Use the busy variable to stop recursive calls.



            case WM_NOTIFY:
            if (lParam)
            {
            NMHDR* header = (NMHDR*)lParam;
            NMLISTVIEW* nmlist = (NMLISTVIEW*)lParam;

            //use `busy` as a flag to prevent recursive calls:
            static BOOL busy = FALSE;
            if (!busy && header->hwndFrom == hListView && header->code == LVN_ITEMCHANGED)
            {
            busy = TRUE;
            if (nmlist->uNewState & LVIS_SELECTED)
            {
            //row has been selected => check the checkbox
            ListView_SetCheckState(hListView, nmlist->iItem, 1);
            }
            else if (nmlist->uNewState & LVIS_STATEIMAGEMASK)
            {
            //checkbox has been changed => select/unselect the row
            BOOL checked = ListView_GetCheckState(hListView, nmlist->iItem);
            ListView_SetItemState(hListView, nmlist->iItem,
            checked ? LVIS_SELECTED : 0, LVIS_SELECTED);
            }
            busy = FALSE;
            }
            }
            break;





            share|improve this answer



























              up vote
              2
              down vote



              accepted










              "whenever a row is selected, the corresponding checkbox will be checked"



              Check WM_NOTIFY and LVIS_SELECTED flag to detect when user selects a row. And use ListView_SetCheckState to check the check-box:



              BOOL CALLBACK DialogProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
              {
              static HWND hListView;
              switch (msg)
              {
              case WM_INITDIALOG:
              hListView = GetDlgItem(hWnd, IDC_LIST1);
              break;

              case WM_NOTIFY:
              {
              NMHDR* header = (NMHDR*)lParam;
              NMLISTVIEW* nmlist = (NMLISTVIEW*)lParam;
              if (header && header->idFrom == IDC_LIST1 && header->code == LVN_ITEMCHANGED)
              if (nmlist->uNewState & LVIS_SELECTED)
              ListView_SetCheckState(hListView, nmlist->iItem, 1);
              break;
              }
              ...
              }


              "whenever a check box is checked that row will be selected"



              Check WM_NOTIFY and LVIS_STATEIMAGEMASK flag to detect when checkbox is checked, then use ListView_SetItemState to select the row.



              Also this can lead to recursive calls, because we change the row in response to checkbox, and we change the checkbox in response to row selection. Use the busy variable to stop recursive calls.



              case WM_NOTIFY:
              if (lParam)
              {
              NMHDR* header = (NMHDR*)lParam;
              NMLISTVIEW* nmlist = (NMLISTVIEW*)lParam;

              //use `busy` as a flag to prevent recursive calls:
              static BOOL busy = FALSE;
              if (!busy && header->hwndFrom == hListView && header->code == LVN_ITEMCHANGED)
              {
              busy = TRUE;
              if (nmlist->uNewState & LVIS_SELECTED)
              {
              //row has been selected => check the checkbox
              ListView_SetCheckState(hListView, nmlist->iItem, 1);
              }
              else if (nmlist->uNewState & LVIS_STATEIMAGEMASK)
              {
              //checkbox has been changed => select/unselect the row
              BOOL checked = ListView_GetCheckState(hListView, nmlist->iItem);
              ListView_SetItemState(hListView, nmlist->iItem,
              checked ? LVIS_SELECTED : 0, LVIS_SELECTED);
              }
              busy = FALSE;
              }
              }
              break;





              share|improve this answer

























                up vote
                2
                down vote



                accepted







                up vote
                2
                down vote



                accepted






                "whenever a row is selected, the corresponding checkbox will be checked"



                Check WM_NOTIFY and LVIS_SELECTED flag to detect when user selects a row. And use ListView_SetCheckState to check the check-box:



                BOOL CALLBACK DialogProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
                {
                static HWND hListView;
                switch (msg)
                {
                case WM_INITDIALOG:
                hListView = GetDlgItem(hWnd, IDC_LIST1);
                break;

                case WM_NOTIFY:
                {
                NMHDR* header = (NMHDR*)lParam;
                NMLISTVIEW* nmlist = (NMLISTVIEW*)lParam;
                if (header && header->idFrom == IDC_LIST1 && header->code == LVN_ITEMCHANGED)
                if (nmlist->uNewState & LVIS_SELECTED)
                ListView_SetCheckState(hListView, nmlist->iItem, 1);
                break;
                }
                ...
                }


                "whenever a check box is checked that row will be selected"



                Check WM_NOTIFY and LVIS_STATEIMAGEMASK flag to detect when checkbox is checked, then use ListView_SetItemState to select the row.



                Also this can lead to recursive calls, because we change the row in response to checkbox, and we change the checkbox in response to row selection. Use the busy variable to stop recursive calls.



                case WM_NOTIFY:
                if (lParam)
                {
                NMHDR* header = (NMHDR*)lParam;
                NMLISTVIEW* nmlist = (NMLISTVIEW*)lParam;

                //use `busy` as a flag to prevent recursive calls:
                static BOOL busy = FALSE;
                if (!busy && header->hwndFrom == hListView && header->code == LVN_ITEMCHANGED)
                {
                busy = TRUE;
                if (nmlist->uNewState & LVIS_SELECTED)
                {
                //row has been selected => check the checkbox
                ListView_SetCheckState(hListView, nmlist->iItem, 1);
                }
                else if (nmlist->uNewState & LVIS_STATEIMAGEMASK)
                {
                //checkbox has been changed => select/unselect the row
                BOOL checked = ListView_GetCheckState(hListView, nmlist->iItem);
                ListView_SetItemState(hListView, nmlist->iItem,
                checked ? LVIS_SELECTED : 0, LVIS_SELECTED);
                }
                busy = FALSE;
                }
                }
                break;





                share|improve this answer














                "whenever a row is selected, the corresponding checkbox will be checked"



                Check WM_NOTIFY and LVIS_SELECTED flag to detect when user selects a row. And use ListView_SetCheckState to check the check-box:



                BOOL CALLBACK DialogProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
                {
                static HWND hListView;
                switch (msg)
                {
                case WM_INITDIALOG:
                hListView = GetDlgItem(hWnd, IDC_LIST1);
                break;

                case WM_NOTIFY:
                {
                NMHDR* header = (NMHDR*)lParam;
                NMLISTVIEW* nmlist = (NMLISTVIEW*)lParam;
                if (header && header->idFrom == IDC_LIST1 && header->code == LVN_ITEMCHANGED)
                if (nmlist->uNewState & LVIS_SELECTED)
                ListView_SetCheckState(hListView, nmlist->iItem, 1);
                break;
                }
                ...
                }


                "whenever a check box is checked that row will be selected"



                Check WM_NOTIFY and LVIS_STATEIMAGEMASK flag to detect when checkbox is checked, then use ListView_SetItemState to select the row.



                Also this can lead to recursive calls, because we change the row in response to checkbox, and we change the checkbox in response to row selection. Use the busy variable to stop recursive calls.



                case WM_NOTIFY:
                if (lParam)
                {
                NMHDR* header = (NMHDR*)lParam;
                NMLISTVIEW* nmlist = (NMLISTVIEW*)lParam;

                //use `busy` as a flag to prevent recursive calls:
                static BOOL busy = FALSE;
                if (!busy && header->hwndFrom == hListView && header->code == LVN_ITEMCHANGED)
                {
                busy = TRUE;
                if (nmlist->uNewState & LVIS_SELECTED)
                {
                //row has been selected => check the checkbox
                ListView_SetCheckState(hListView, nmlist->iItem, 1);
                }
                else if (nmlist->uNewState & LVIS_STATEIMAGEMASK)
                {
                //checkbox has been changed => select/unselect the row
                BOOL checked = ListView_GetCheckState(hListView, nmlist->iItem);
                ListView_SetItemState(hListView, nmlist->iItem,
                checked ? LVIS_SELECTED : 0, LVIS_SELECTED);
                }
                busy = FALSE;
                }
                }
                break;






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited May 23 '16 at 4:59

























                answered May 21 '16 at 4:52









                Barmak Shemirani

                20.3k42044




                20.3k42044






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37356042%2fhow-to-sync-listview-checkbox-with-selectionwin32%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini