// Checks/Unchecks items depending on the give bitvalue private void UpdateCheckedItems(int value) { IsUpdatingCheckStates = true; // Iterate over all items for (int i = 0; i < Items.Count; i++) { FlagCheckedListBoxItem item = Items[i] as FlagCheckedListBoxItem; if (item == null) { continue; } if (item.Value == 0) { SetItemChecked(i, value == 0); } else { // If the bit for the current item is on in the bitvalue, check it if (((item.Value & value) == item.Value) && (item.Value != 0)) { SetItemChecked(i, true); } // Otherwise uncheck it else { SetItemChecked(i, false); } } } IsUpdatingCheckStates = false; }
// Updates items in the checklistbox // composite = The item that was checked/unchecked // cs = The check state of that item private void UpdateCheckedItems(FlagCheckedListBoxItem composite, CheckState cs) { // If the value of the item is 0, call directly. if (composite.Value == 0) { UpdateCheckedItems(0); } // Get the total value of all checked items int sum = (from object t in Items select t as FlagCheckedListBoxItem).Where((item, i) => (item != null) && GetItemChecked(i)).Aggregate(0, (current, item) => current | item.Value); // If the item has been unchecked, remove its bits from the sum if (cs == CheckState.Unchecked) { sum = sum & ~composite.Value; } // If the item has been checked, combine its bits with the sum else { sum |= composite.Value; } // Update all items in the checklistbox based on the final bit value UpdateCheckedItems(sum); }
protected override void OnItemCheck(ItemCheckEventArgs e) { base.OnItemCheck(e); if (IsUpdatingCheckStates) { return; } // Get the checked/unchecked item FlagCheckedListBoxItem item = Items[e.Index] as FlagCheckedListBoxItem; // Update other items UpdateCheckedItems(item, e.NewValue); }
// Adds an integer value and its associated description private void Add(int v, string c) { FlagCheckedListBoxItem item = new FlagCheckedListBoxItem(v, c); Items.Add(item); }