How to add a checkbox to the listcontrol column header to check and uncheck all checkboxes of the list items?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















Currently I am working on a listcontrol,where the first column header should consists of a checkbox.Adding checkbox to the list items can be done by setting the style to LVS_EX_CHECKBOXES.Is there any way to add checkbox to the header so that if I check it all the items in the list should get checked and if I uncheck it all the list items should get unchecked.



Can anyone please let me know any possible way of doing so.










share|improve this question




























    1















    Currently I am working on a listcontrol,where the first column header should consists of a checkbox.Adding checkbox to the list items can be done by setting the style to LVS_EX_CHECKBOXES.Is there any way to add checkbox to the header so that if I check it all the items in the list should get checked and if I uncheck it all the list items should get unchecked.



    Can anyone please let me know any possible way of doing so.










    share|improve this question
























      1












      1








      1


      1






      Currently I am working on a listcontrol,where the first column header should consists of a checkbox.Adding checkbox to the list items can be done by setting the style to LVS_EX_CHECKBOXES.Is there any way to add checkbox to the header so that if I check it all the items in the list should get checked and if I uncheck it all the list items should get unchecked.



      Can anyone please let me know any possible way of doing so.










      share|improve this question














      Currently I am working on a listcontrol,where the first column header should consists of a checkbox.Adding checkbox to the list items can be done by setting the style to LVS_EX_CHECKBOXES.Is there any way to add checkbox to the header so that if I check it all the items in the list should get checked and if I uncheck it all the list items should get unchecked.



      Can anyone please let me know any possible way of doing so.







      visual-c++ mfc






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 5 '14 at 13:35









      SivaSiva

      458826




      458826






















          2 Answers
          2






          active

          oldest

          votes


















          1














          Follow the below steps to get the checkbox on the header and using that checkbox on the header we can check and uncheck all the items in the list.



          In the OnInitDialog() Add this below piece of code:



          BOOL OnInitDialog()
          LVCF_TEXT


          Add this event HDN_ITEMSTATEICONCLICK



           void CMFPSearchListView::OnHdnItemStateIconClickListctrl(NMHDR *pNMHDR, LRESULT *pResult)

          LPNMHEADER pNMHeader = (LPNMHEADER)pNMHDR;

          // first determine whether the click was a checkbox change
          if (pNMHeader->pitem->mask & HDI_FORMAT && pNMHeader->pitem->fmt & HDF_CHECKBOX)

          // now determine whether it was checked or unchecked
          BOOL bUnChecked = pNMHeader->pitem->fmt & HDF_CHECKED;

          // apply check state to each list item
          for (int nItem = 0; nItem < m_listCtrl.GetItemCount(); nItem++)
          m_listCtrl.SetCheck(nItem, !bUnChecked);



          *pResult = 1; //if pResult = 0 then you will get blue color selection on the items when you check header checkbox , in order to avoid that I made pResult = 1; Now we won't face the selection issue.



          //Add LVN_ITEMCHANGED



          void OnListViewItemchanged(NMHDR *pNMHDR, LRESULT *pResult)
          LVCF_WIDTH


          Note:This header checkbox will work from above Windows XP Operating system.






          share|improve this answer























          • May be this would be better rephrased as ""will work beginning in the Windows Vista Operating System".

            – sergiol
            Dec 9 '16 at 18:51











          • I have the problem reported on neowin.net/forum/topic/819824-c-listview : Checkbox on header does not appear without having the extended style LVS_EX_AUTOCHECKSELECT and I want to NOT have it.

            – sergiol
            Dec 12 '16 at 17:47











          • Another observation: The combobox appears for CListCtrl/CHeaderCtrl, but not for CMFCListCtrl/CMFCHeaderCtrl! GRRRRR

            – sergiol
            Dec 12 '16 at 18:30











          • In addition to my previous comment, it is very likely the feature of having a checkbox on header for CMFCListCtrl/CMFCHeaderCtrlis not even implemented, as I don't find occurences of HDS_CHECKBOXES, HDF_CHECKBOX and HDF_CHECKED on the afxheaderctrl.cpp|h and afxlistctrl.cpp|h files!

            – sergiol
            Dec 12 '16 at 18:54











          • My problem is solved by the stackoverflow.com/a/41205752/383779 to my question!

            – sergiol
            Dec 18 '16 at 18:18


















          0














          The accepted answer was very helpful, but using LVCOLUMN did not worked well for me.



          so I found out using HDITEM working better for me:



          CHeaderCtrl* pHeaderControl = m_ListControl.GetHeaderCtrl();

          HDITEM hdi = 0 ;
          hdi.mask = HDI_FORMAT;
          pHeaderControl->GetItem(0, &hdi);
          hdi.fmt |= HDF_CHECKBOX;

          if (bAllChecked)
          = HDF_CHECKED;

          else

          hdi.fmt &= ~HDF_CHECKED;

          pHeaderControl->SetItem(0, &hdi);





          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',
            autoActivateHeartbeat: false,
            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%2f25687172%2fhow-to-add-a-checkbox-to-the-listcontrol-column-header-to-check-and-uncheck-all%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Follow the below steps to get the checkbox on the header and using that checkbox on the header we can check and uncheck all the items in the list.



            In the OnInitDialog() Add this below piece of code:



            BOOL OnInitDialog()
            LVCF_TEXT


            Add this event HDN_ITEMSTATEICONCLICK



             void CMFPSearchListView::OnHdnItemStateIconClickListctrl(NMHDR *pNMHDR, LRESULT *pResult)

            LPNMHEADER pNMHeader = (LPNMHEADER)pNMHDR;

            // first determine whether the click was a checkbox change
            if (pNMHeader->pitem->mask & HDI_FORMAT && pNMHeader->pitem->fmt & HDF_CHECKBOX)

            // now determine whether it was checked or unchecked
            BOOL bUnChecked = pNMHeader->pitem->fmt & HDF_CHECKED;

            // apply check state to each list item
            for (int nItem = 0; nItem < m_listCtrl.GetItemCount(); nItem++)
            m_listCtrl.SetCheck(nItem, !bUnChecked);



            *pResult = 1; //if pResult = 0 then you will get blue color selection on the items when you check header checkbox , in order to avoid that I made pResult = 1; Now we won't face the selection issue.



            //Add LVN_ITEMCHANGED



            void OnListViewItemchanged(NMHDR *pNMHDR, LRESULT *pResult)
            LVCF_WIDTH


            Note:This header checkbox will work from above Windows XP Operating system.






            share|improve this answer























            • May be this would be better rephrased as ""will work beginning in the Windows Vista Operating System".

              – sergiol
              Dec 9 '16 at 18:51











            • I have the problem reported on neowin.net/forum/topic/819824-c-listview : Checkbox on header does not appear without having the extended style LVS_EX_AUTOCHECKSELECT and I want to NOT have it.

              – sergiol
              Dec 12 '16 at 17:47











            • Another observation: The combobox appears for CListCtrl/CHeaderCtrl, but not for CMFCListCtrl/CMFCHeaderCtrl! GRRRRR

              – sergiol
              Dec 12 '16 at 18:30











            • In addition to my previous comment, it is very likely the feature of having a checkbox on header for CMFCListCtrl/CMFCHeaderCtrlis not even implemented, as I don't find occurences of HDS_CHECKBOXES, HDF_CHECKBOX and HDF_CHECKED on the afxheaderctrl.cpp|h and afxlistctrl.cpp|h files!

              – sergiol
              Dec 12 '16 at 18:54











            • My problem is solved by the stackoverflow.com/a/41205752/383779 to my question!

              – sergiol
              Dec 18 '16 at 18:18















            1














            Follow the below steps to get the checkbox on the header and using that checkbox on the header we can check and uncheck all the items in the list.



            In the OnInitDialog() Add this below piece of code:



            BOOL OnInitDialog()
            LVCF_TEXT


            Add this event HDN_ITEMSTATEICONCLICK



             void CMFPSearchListView::OnHdnItemStateIconClickListctrl(NMHDR *pNMHDR, LRESULT *pResult)

            LPNMHEADER pNMHeader = (LPNMHEADER)pNMHDR;

            // first determine whether the click was a checkbox change
            if (pNMHeader->pitem->mask & HDI_FORMAT && pNMHeader->pitem->fmt & HDF_CHECKBOX)

            // now determine whether it was checked or unchecked
            BOOL bUnChecked = pNMHeader->pitem->fmt & HDF_CHECKED;

            // apply check state to each list item
            for (int nItem = 0; nItem < m_listCtrl.GetItemCount(); nItem++)
            m_listCtrl.SetCheck(nItem, !bUnChecked);



            *pResult = 1; //if pResult = 0 then you will get blue color selection on the items when you check header checkbox , in order to avoid that I made pResult = 1; Now we won't face the selection issue.



            //Add LVN_ITEMCHANGED



            void OnListViewItemchanged(NMHDR *pNMHDR, LRESULT *pResult)
            LVCF_WIDTH


            Note:This header checkbox will work from above Windows XP Operating system.






            share|improve this answer























            • May be this would be better rephrased as ""will work beginning in the Windows Vista Operating System".

              – sergiol
              Dec 9 '16 at 18:51











            • I have the problem reported on neowin.net/forum/topic/819824-c-listview : Checkbox on header does not appear without having the extended style LVS_EX_AUTOCHECKSELECT and I want to NOT have it.

              – sergiol
              Dec 12 '16 at 17:47











            • Another observation: The combobox appears for CListCtrl/CHeaderCtrl, but not for CMFCListCtrl/CMFCHeaderCtrl! GRRRRR

              – sergiol
              Dec 12 '16 at 18:30











            • In addition to my previous comment, it is very likely the feature of having a checkbox on header for CMFCListCtrl/CMFCHeaderCtrlis not even implemented, as I don't find occurences of HDS_CHECKBOXES, HDF_CHECKBOX and HDF_CHECKED on the afxheaderctrl.cpp|h and afxlistctrl.cpp|h files!

              – sergiol
              Dec 12 '16 at 18:54











            • My problem is solved by the stackoverflow.com/a/41205752/383779 to my question!

              – sergiol
              Dec 18 '16 at 18:18













            1












            1








            1







            Follow the below steps to get the checkbox on the header and using that checkbox on the header we can check and uncheck all the items in the list.



            In the OnInitDialog() Add this below piece of code:



            BOOL OnInitDialog()
            LVCF_TEXT


            Add this event HDN_ITEMSTATEICONCLICK



             void CMFPSearchListView::OnHdnItemStateIconClickListctrl(NMHDR *pNMHDR, LRESULT *pResult)

            LPNMHEADER pNMHeader = (LPNMHEADER)pNMHDR;

            // first determine whether the click was a checkbox change
            if (pNMHeader->pitem->mask & HDI_FORMAT && pNMHeader->pitem->fmt & HDF_CHECKBOX)

            // now determine whether it was checked or unchecked
            BOOL bUnChecked = pNMHeader->pitem->fmt & HDF_CHECKED;

            // apply check state to each list item
            for (int nItem = 0; nItem < m_listCtrl.GetItemCount(); nItem++)
            m_listCtrl.SetCheck(nItem, !bUnChecked);



            *pResult = 1; //if pResult = 0 then you will get blue color selection on the items when you check header checkbox , in order to avoid that I made pResult = 1; Now we won't face the selection issue.



            //Add LVN_ITEMCHANGED



            void OnListViewItemchanged(NMHDR *pNMHDR, LRESULT *pResult)
            LVCF_WIDTH


            Note:This header checkbox will work from above Windows XP Operating system.






            share|improve this answer













            Follow the below steps to get the checkbox on the header and using that checkbox on the header we can check and uncheck all the items in the list.



            In the OnInitDialog() Add this below piece of code:



            BOOL OnInitDialog()
            LVCF_TEXT


            Add this event HDN_ITEMSTATEICONCLICK



             void CMFPSearchListView::OnHdnItemStateIconClickListctrl(NMHDR *pNMHDR, LRESULT *pResult)

            LPNMHEADER pNMHeader = (LPNMHEADER)pNMHDR;

            // first determine whether the click was a checkbox change
            if (pNMHeader->pitem->mask & HDI_FORMAT && pNMHeader->pitem->fmt & HDF_CHECKBOX)

            // now determine whether it was checked or unchecked
            BOOL bUnChecked = pNMHeader->pitem->fmt & HDF_CHECKED;

            // apply check state to each list item
            for (int nItem = 0; nItem < m_listCtrl.GetItemCount(); nItem++)
            m_listCtrl.SetCheck(nItem, !bUnChecked);



            *pResult = 1; //if pResult = 0 then you will get blue color selection on the items when you check header checkbox , in order to avoid that I made pResult = 1; Now we won't face the selection issue.



            //Add LVN_ITEMCHANGED



            void OnListViewItemchanged(NMHDR *pNMHDR, LRESULT *pResult)
            LVCF_WIDTH


            Note:This header checkbox will work from above Windows XP Operating system.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Sep 16 '14 at 14:50









            SivaSiva

            458826




            458826












            • May be this would be better rephrased as ""will work beginning in the Windows Vista Operating System".

              – sergiol
              Dec 9 '16 at 18:51











            • I have the problem reported on neowin.net/forum/topic/819824-c-listview : Checkbox on header does not appear without having the extended style LVS_EX_AUTOCHECKSELECT and I want to NOT have it.

              – sergiol
              Dec 12 '16 at 17:47











            • Another observation: The combobox appears for CListCtrl/CHeaderCtrl, but not for CMFCListCtrl/CMFCHeaderCtrl! GRRRRR

              – sergiol
              Dec 12 '16 at 18:30











            • In addition to my previous comment, it is very likely the feature of having a checkbox on header for CMFCListCtrl/CMFCHeaderCtrlis not even implemented, as I don't find occurences of HDS_CHECKBOXES, HDF_CHECKBOX and HDF_CHECKED on the afxheaderctrl.cpp|h and afxlistctrl.cpp|h files!

              – sergiol
              Dec 12 '16 at 18:54











            • My problem is solved by the stackoverflow.com/a/41205752/383779 to my question!

              – sergiol
              Dec 18 '16 at 18:18

















            • May be this would be better rephrased as ""will work beginning in the Windows Vista Operating System".

              – sergiol
              Dec 9 '16 at 18:51











            • I have the problem reported on neowin.net/forum/topic/819824-c-listview : Checkbox on header does not appear without having the extended style LVS_EX_AUTOCHECKSELECT and I want to NOT have it.

              – sergiol
              Dec 12 '16 at 17:47











            • Another observation: The combobox appears for CListCtrl/CHeaderCtrl, but not for CMFCListCtrl/CMFCHeaderCtrl! GRRRRR

              – sergiol
              Dec 12 '16 at 18:30











            • In addition to my previous comment, it is very likely the feature of having a checkbox on header for CMFCListCtrl/CMFCHeaderCtrlis not even implemented, as I don't find occurences of HDS_CHECKBOXES, HDF_CHECKBOX and HDF_CHECKED on the afxheaderctrl.cpp|h and afxlistctrl.cpp|h files!

              – sergiol
              Dec 12 '16 at 18:54











            • My problem is solved by the stackoverflow.com/a/41205752/383779 to my question!

              – sergiol
              Dec 18 '16 at 18:18
















            May be this would be better rephrased as ""will work beginning in the Windows Vista Operating System".

            – sergiol
            Dec 9 '16 at 18:51





            May be this would be better rephrased as ""will work beginning in the Windows Vista Operating System".

            – sergiol
            Dec 9 '16 at 18:51













            I have the problem reported on neowin.net/forum/topic/819824-c-listview : Checkbox on header does not appear without having the extended style LVS_EX_AUTOCHECKSELECT and I want to NOT have it.

            – sergiol
            Dec 12 '16 at 17:47





            I have the problem reported on neowin.net/forum/topic/819824-c-listview : Checkbox on header does not appear without having the extended style LVS_EX_AUTOCHECKSELECT and I want to NOT have it.

            – sergiol
            Dec 12 '16 at 17:47













            Another observation: The combobox appears for CListCtrl/CHeaderCtrl, but not for CMFCListCtrl/CMFCHeaderCtrl! GRRRRR

            – sergiol
            Dec 12 '16 at 18:30





            Another observation: The combobox appears for CListCtrl/CHeaderCtrl, but not for CMFCListCtrl/CMFCHeaderCtrl! GRRRRR

            – sergiol
            Dec 12 '16 at 18:30













            In addition to my previous comment, it is very likely the feature of having a checkbox on header for CMFCListCtrl/CMFCHeaderCtrlis not even implemented, as I don't find occurences of HDS_CHECKBOXES, HDF_CHECKBOX and HDF_CHECKED on the afxheaderctrl.cpp|h and afxlistctrl.cpp|h files!

            – sergiol
            Dec 12 '16 at 18:54





            In addition to my previous comment, it is very likely the feature of having a checkbox on header for CMFCListCtrl/CMFCHeaderCtrlis not even implemented, as I don't find occurences of HDS_CHECKBOXES, HDF_CHECKBOX and HDF_CHECKED on the afxheaderctrl.cpp|h and afxlistctrl.cpp|h files!

            – sergiol
            Dec 12 '16 at 18:54













            My problem is solved by the stackoverflow.com/a/41205752/383779 to my question!

            – sergiol
            Dec 18 '16 at 18:18





            My problem is solved by the stackoverflow.com/a/41205752/383779 to my question!

            – sergiol
            Dec 18 '16 at 18:18













            0














            The accepted answer was very helpful, but using LVCOLUMN did not worked well for me.



            so I found out using HDITEM working better for me:



            CHeaderCtrl* pHeaderControl = m_ListControl.GetHeaderCtrl();

            HDITEM hdi = 0 ;
            hdi.mask = HDI_FORMAT;
            pHeaderControl->GetItem(0, &hdi);
            hdi.fmt |= HDF_CHECKBOX;

            if (bAllChecked)
            = HDF_CHECKED;

            else

            hdi.fmt &= ~HDF_CHECKED;

            pHeaderControl->SetItem(0, &hdi);





            share|improve this answer



























              0














              The accepted answer was very helpful, but using LVCOLUMN did not worked well for me.



              so I found out using HDITEM working better for me:



              CHeaderCtrl* pHeaderControl = m_ListControl.GetHeaderCtrl();

              HDITEM hdi = 0 ;
              hdi.mask = HDI_FORMAT;
              pHeaderControl->GetItem(0, &hdi);
              hdi.fmt |= HDF_CHECKBOX;

              if (bAllChecked)
              = HDF_CHECKED;

              else

              hdi.fmt &= ~HDF_CHECKED;

              pHeaderControl->SetItem(0, &hdi);





              share|improve this answer

























                0












                0








                0







                The accepted answer was very helpful, but using LVCOLUMN did not worked well for me.



                so I found out using HDITEM working better for me:



                CHeaderCtrl* pHeaderControl = m_ListControl.GetHeaderCtrl();

                HDITEM hdi = 0 ;
                hdi.mask = HDI_FORMAT;
                pHeaderControl->GetItem(0, &hdi);
                hdi.fmt |= HDF_CHECKBOX;

                if (bAllChecked)
                = HDF_CHECKED;

                else

                hdi.fmt &= ~HDF_CHECKED;

                pHeaderControl->SetItem(0, &hdi);





                share|improve this answer













                The accepted answer was very helpful, but using LVCOLUMN did not worked well for me.



                so I found out using HDITEM working better for me:



                CHeaderCtrl* pHeaderControl = m_ListControl.GetHeaderCtrl();

                HDITEM hdi = 0 ;
                hdi.mask = HDI_FORMAT;
                pHeaderControl->GetItem(0, &hdi);
                hdi.fmt |= HDF_CHECKBOX;

                if (bAllChecked)
                = HDF_CHECKED;

                else

                hdi.fmt &= ~HDF_CHECKED;

                pHeaderControl->SetItem(0, &hdi);






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 15 '18 at 15:49









                wulfithewulfwulfithewulf

                123




                123



























                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f25687172%2fhow-to-add-a-checkbox-to-the-listcontrol-column-header-to-check-and-uncheck-all%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







                    Popular posts from this blog

                    Use pre created SQLite database for Android project in kotlin

                    Ruanda

                    Darth Vader #20