Quantcast
Viewing all articles
Browse latest Browse all 12583

what is the best way to imlement checking items in a checkedlistbox

Hi

I have a checkedlistbox with some iteams and "All" item, like this:

  O  All
  O Item1
  O Item 2
  O Item 3
  O ...

Now, I want to do the follow actions:

  1. check all iteams if item "All" is checked, and uncheack all iteams if item "All" is unchecked

  2. if "All" is checked and the user uncheck ItemX "All" will be unchecked as well. 

  3. Check "All" if the user check all Items (and "All" was unchecked)

I write this code (for 1-2, #3 I haven't done yet):

       private void clb_FilterComponet_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (dontRunHandler) return;
            dontRunHandler = true;
            if (e.Index == 0)   // check or uncheck "all"
            {
                bool toCheck = true;
                if (e.NewValue == CheckState.Unchecked) toCheck = false;
                for (int i = 1; i < clb_FilterComponet.Items.Count; i++)
                    clb_FilterComponet.SetItemChecked(i, toCheck);
            }
            else if (e.NewValue == CheckState.Unchecked)    // remove All v
                clb_FilterComponet.SetItemCheckState(0, CheckState.Unchecked);
            dontRunHandler = false;
        }

My question is, is there another way, more effective, to perfrom it ?

Thank you!




Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>