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 = 0;

            for (int i = 0; i < Items.Count; i++)
            {
                FlagCheckedListBoxItem item = Items[i] as FlagCheckedListBoxItem;

                // If item is checked, add its value to the sum.
                if (GetItemChecked(i))
                {
                    sum |= item.Value;
                }
            }

            // If the item has been unchecked, remove its bits from the sum
            if (cs == CheckState.Unchecked)
            {
                sum = sum & (~composite.Value);
            }
            else // If the item has been checked, combine its bits with the sum
            {
                sum |= composite.Value;
            }

            // Update all items in the checklistbox based on the final bit value
            UpdateCheckedItems(sum);
        }
        /// <summary>
        /// Adds a new item to the list.
        /// </summary>
        /// <param name="value">Value of the new item.</param>
        /// <param name="caption">Caption of the new item.</param>
        /// <returns>The new item.</returns>
        public FlagCheckedListBoxItem Add(int value, string caption)
        {
            FlagCheckedListBoxItem item = new FlagCheckedListBoxItem(value, caption);

            Items.Add(item);
            return(item);
        }
        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.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);
                    }
                    else // Otherwise uncheck it
                    {
                        SetItemChecked(i, false);
                    }
                }
            }

            _isUpdatingCheckStates = false;
        }
        /// <summary>
        /// Handles the <see cref="CheckedListBox.ItemCheck"/> event.
        /// </summary>
        /// <param name="e">Event arguments</param>
        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);
        }
        /// <summary>
        /// Gets the current bit value corresponding to all checked items
        /// </summary>
        /// <returns></returns>
        public int GetCurrentValue()
        {
            int sum = 0;

            for (int i = 0; i < Items.Count; i++)
            {
                FlagCheckedListBoxItem item = Items[i] as FlagCheckedListBoxItem;

                if (GetItemChecked(i))
                {
                    sum |= item.Value;
                }
            }

            return(sum);
        }
 /// <summary>
 /// Adds an item to the list.
 /// </summary>
 /// <param name="item">The item.</param>
 /// <returns>The item.</returns>
 public FlagCheckedListBoxItem Add(FlagCheckedListBoxItem item)
 {
     Items.Add(item);
     return(item);
 }
 /// <summary>
 /// Adds an item to the list.
 /// </summary>
 /// <param name="item">The item.</param>
 /// <returns>The item.</returns>
 public FlagCheckedListBoxItem Add(FlagCheckedListBoxItem item)
 {
     Items.Add(item);
     return item;
 }
 /// <summary>
 /// Adds a new item to the list.
 /// </summary>
 /// <param name="value">Value of the new item.</param>
 /// <param name="caption">Caption of the new item.</param>
 /// <returns>The new item.</returns>
 public FlagCheckedListBoxItem Add(int value, string caption)
 {
     FlagCheckedListBoxItem item = new FlagCheckedListBoxItem(value, caption);
     Items.Add(item);
     return 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 = 0;
            for (int i = 0; i < Items.Count; i++)
            {
                FlagCheckedListBoxItem item = Items[i] as FlagCheckedListBoxItem;

                // If item is checked, add its value to the sum.
                if (GetItemChecked(i))
                    sum |= item.Value;
            }

            // If the item has been unchecked, remove its bits from the sum
            if (cs == CheckState.Unchecked)
                sum = sum & (~composite.Value);
            else // If the item has been checked, combine its bits with the sum
                sum |= composite.Value;

            // Update all items in the checklistbox based on the final bit value
            UpdateCheckedItems(sum);
        }