/// <summary>
        /// Updates the checked items.
        /// </summary>
        /// <param name="composite">The composite.</param>
        /// <param name="cs">The cs.</param>
        private void UpdateCheckedItems(ZeroitFlagCheckedListBoxItem 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++)
            {
                ZeroitFlagCheckedListBoxItem item = Items[i] as ZeroitFlagCheckedListBoxItem;

                // 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>
        /// Updates the checked items.
        /// </summary>
        /// <param name="value">The value.</param>
        private void UpdateCheckedItems(int value)
        {
            _isUpdatingCheckStates = true;

            // Iterate over all items
            for (int i = 0; i < Items.Count; i++)
            {
                ZeroitFlagCheckedListBoxItem item = Items[i] as ZeroitFlagCheckedListBoxItem;

                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>
        /// 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 ZeroitFlagCheckedListBoxItem Add(int value, string caption)
        {
            ZeroitFlagCheckedListBoxItem item = new ZeroitFlagCheckedListBoxItem(value, caption);

            Items.Add(item);
            return(item);
        }
        /// <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
            ZeroitFlagCheckedListBoxItem item = Items[e.Index] as ZeroitFlagCheckedListBoxItem;

            // Update other items
            UpdateCheckedItems(item, e.NewValue);
        }
        /// <summary>
        /// Gets the current bit value corresponding to all checked items
        /// </summary>
        /// <returns>System.Int32.</returns>
        public int GetCurrentValue()
        {
            int sum = 0;

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

                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 ZeroitFlagCheckedListBoxItem Add(ZeroitFlagCheckedListBoxItem item)
 {
     Items.Add(item);
     return(item);
 }