Exemple #1
0
        public void SynchroniseControlsWithComboBoxItems()
        {
            SuspendLayout();
            if (_checkBoxComboBox.MustAddHiddenItem)
            {
                _checkBoxComboBox.Items.Insert(
                    0, _checkBoxComboBox.GetCSVText(false));                     // INVISIBLE ITEM
                _checkBoxComboBox.SelectedIndex     = 0;
                _checkBoxComboBox.MustAddHiddenItem = false;
            }
            Controls.Clear();

            for (var index = _items.Count - 1; index >= 0; index--)
            {
                var item = _items[index];
                if (!_checkBoxComboBox.Items.Contains(item.ComboBoxItem))
                {
                    _items.Remove(item);
                    item.Dispose();
                }
            }

            var hasHiddenItem =
                _checkBoxComboBox.DropDownStyle == ComboBoxStyle.DropDownList &&
                _checkBoxComboBox.DataSource == null &&
                !DesignMode;

            var newList = new CheckBoxComboBoxItemList(_checkBoxComboBox);

            for (var index0 = 0; index0 <= _checkBoxComboBox.Items.Count - 1; index0++)
            {
                var Object = _checkBoxComboBox.Items[index0];
                CheckBoxComboBoxItem item = null;
                // The hidden item could match any other item when only
                // one other item was selected.
                if (index0 == 0 && hasHiddenItem && _items.Count > 0)
                {
                    item = _items[0];
                }
                else
                {
                    var startIndex = hasHiddenItem
                                                ? 1 // Skip the hidden item, it could match
                                                : 0;
                    for (var index1 = startIndex; index1 <= _items.Count - 1; index1++)
                    {
                        if (_items[index1].ComboBoxItem != Object)
                        {
                            continue;
                        }

                        item = _items[index1];
                        break;
                    }
                }
                if (item == null)
                {
                    item = new CheckBoxComboBoxItem(_checkBoxComboBox, Object);
                    item.ApplyProperties(_checkBoxComboBox.CheckBoxProperties);
                }
                newList.Add(item);
                item.Dock = DockStyle.Top;
            }
            _items.Clear();
            _items.AddRange(newList);

            if (newList.Count > 0)
            {
                // This reverse helps to maintain correct docking order.
                newList.Reverse();
                // If you get an error here that "Cannot convert to the desired
                // type, it probably means the controls are not binding correctly.
                // The Checked property is binded to the ValueMember property.
                // It must be a bool for example.
                // ReSharper disable once CoVariantArrayConversion
                Controls.AddRange(newList.ToArray());
            }

            // Keep the first item invisible
            if (_checkBoxComboBox.DropDownStyle == ComboBoxStyle.DropDownList &&
                _checkBoxComboBox.DataSource == null &&
                !DesignMode)
            {
                _checkBoxComboBox.CheckBoxItems[0].Visible = false;
            }

            ResumeLayout();
        }
        /// <summary>
        /// Maintains the controls displayed in the list by keeping them in sync with the actual
        /// items in the combobox. (e.g. removing and adding as well as ordering)
        /// </summary>
        public void SynchroniseControlsWithComboBoxItems()
        {
            SuspendLayout();
            if (_CheckBoxComboBox._MustAddHiddenItem)
            {
                _CheckBoxComboBox.Items.Insert(
                    0, _CheckBoxComboBox.GetCSVText(false)); // INVISIBLE ITEM
                _CheckBoxComboBox.SelectedIndex      = 0;
                _CheckBoxComboBox._MustAddHiddenItem = false;
            }
            Controls.Clear();
            #region Disposes all items that are no longer in the combo box list

            for (int Index = _Items.Count - 1; Index >= 0; Index--)
            {
                CheckBoxComboBoxItem Item = _Items[Index];
                if (!_CheckBoxComboBox.Items.Contains(Item.ComboBoxItem))
                {
                    _Items.Remove(Item);
                    Item.Dispose();
                }
            }

            #endregion
            #region Recreate the list in the same order of the combo box items

            bool HasHiddenItem =
                _CheckBoxComboBox.DropDownStyle == ComboBoxStyle.DropDownList &&
                _CheckBoxComboBox.DataSource == null &&
                !DesignMode;

            CheckBoxComboBoxItemList NewList = new CheckBoxComboBoxItemList(_CheckBoxComboBox);
            for (int Index0 = 0; Index0 <= _CheckBoxComboBox.Items.Count - 1; Index0++)
            {
                object Object             = _CheckBoxComboBox.Items[Index0];
                CheckBoxComboBoxItem Item = null;
                // The hidden item could match any other item when only
                // one other item was selected.
                if (Index0 == 0 && HasHiddenItem && _Items.Count > 0)
                {
                    Item = _Items[0];
                }
                else
                {
                    int StartIndex = HasHiddenItem
                        ? 1 // Skip the hidden item, it could match
                        : 0;
                    for (int Index1 = StartIndex; Index1 <= _Items.Count - 1; Index1++)
                    {
                        if (_Items[Index1].ComboBoxItem == Object)
                        {
                            Item = _Items[Index1];
                            break;
                        }
                    }
                }
                if (Item == null)
                {
                    Item = new CheckBoxComboBoxItem(_CheckBoxComboBox, Object);
                    Item.ApplyProperties(_CheckBoxComboBox.CheckBoxProperties);
                }
                NewList.Add(Item);
                Item.Dock = DockStyle.Top;
            }
            _Items.Clear();
            _Items.AddRange(NewList);

            #endregion
            #region Add the items to the controls in reversed order to maintain correct docking order

            if (NewList.Count > 0)
            {
                // This reverse helps to maintain correct docking order.
                NewList.Reverse();
                // If you get an error here that "Cannot convert to the desired
                // type, it probably means the controls are not binding correctly.
                // The Checked property is binded to the ValueMember property.
                // It must be a bool for example.
                Controls.AddRange(NewList.ToArray());
            }

            #endregion

            // Keep the first item invisible
            if (_CheckBoxComboBox.DropDownStyle == ComboBoxStyle.DropDownList &&
                _CheckBoxComboBox.DataSource == null &&
                !DesignMode)
            {
                _CheckBoxComboBox.CheckBoxItems[0].Visible = false;
            }

            ResumeLayout();
        }