Example #1
0
        public CheckBoxComboBoxListControl(MultiSelectComboBox owner)
            : base()
        {
            DoubleBuffered    = true;
            _CheckBoxComboBox = owner;
            _Items            = new CheckBoxComboBoxItemList(_CheckBoxComboBox);
            BackColor         = SystemColors.Window;

            AutoScroll   = true;
            ResizeRedraw = true;

            MinimumSize = new Size(1, 1);
            MaximumSize = new Size(500, 500);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="CheckBoxComboBoxListControl"/> class.
        /// </summary>
        /// <param name="owner">
        /// The owner.
        /// </param>
        public CheckBoxComboBoxListControl(CheckBoxComboBox owner)
        {
            this.DoubleBuffered = true;
            this._CheckBoxComboBox = owner;
            this._Items = new CheckBoxComboBoxItemList(this._CheckBoxComboBox);
            this.BackColor = SystemColors.Window;

            // AutoScaleMode = AutoScaleMode.Inherit;
            this.AutoScroll = true;
            this.ResizeRedraw = true;

            // if you don't set this, a Resize operation causes an error in the base class.
            this.MinimumSize = new Size(1, 1);
            this.MaximumSize = new Size(500, 500);
        }
        /// <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()
        {
            this.SuspendLayout();
            if (this._CheckBoxComboBox._MustAddHiddenItem)
            {
                this._CheckBoxComboBox.Items.Insert(0, this._CheckBoxComboBox.GetCSVText(false)); // INVISIBLE ITEM
                this._CheckBoxComboBox.SelectedIndex = 0;
                this._CheckBoxComboBox._MustAddHiddenItem = false;
            }

            this.Controls.Clear();

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

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

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

            var NewList = new CheckBoxComboBoxItemList(this._CheckBoxComboBox);
            for (int Index0 = 0; Index0 <= this._CheckBoxComboBox.Items.Count - 1; Index0++)
            {
                object Object = this._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 && this._Items.Count > 0)
                {
                    Item = this._Items[0];
                }
                else
                {
                    int StartIndex = HasHiddenItem
                                         ? 1 // Skip the hidden item, it could match
                                         : 0;
                    for (int Index1 = StartIndex; Index1 <= this._Items.Count - 1; Index1++)
                    {
                        if (this._Items[Index1].ComboBoxItem == Object)
                        {
                            Item = this._Items[Index1];
                            break;
                        }
                    }
                }

                if (Item == null)
                {
                    Item = new CheckBoxComboBoxItem(this._CheckBoxComboBox, Object);
                    Item.ApplyProperties(this._CheckBoxComboBox.CheckBoxProperties);
                }

                NewList.Add(Item);
                Item.Dock = DockStyle.Top;
            }

            this._Items.Clear();
            this._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.
                this.Controls.AddRange(NewList.ToArray());
            }

            #endregion

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

            this.ResumeLayout();
        }
Example #4
0
        public void SynchroniseControlsWithComboBoxItems()
        {
            SuspendLayout();
            if (_CheckBoxComboBox._MustAddHiddenItem)
            {
                _CheckBoxComboBox.Items.Insert(
                    0, _CheckBoxComboBox.GetCSVText(false));
                _CheckBoxComboBox.SelectedIndex      = 0;
                _CheckBoxComboBox._MustAddHiddenItem = false;
            }
            Controls.Clear();

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

            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;

                if (Index0 == 0 && HasHiddenItem && _Items.Count > 0)
                {
                    Item = _Items[0];
                }
                else
                {
                    int StartIndex = HasHiddenItem ? 1 : 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);

            if (NewList.Count > 0)
            {
                NewList.Reverse();

                Controls.AddRange(NewList.ToArray());
            }

            if (_CheckBoxComboBox.DropDownStyle == ComboBoxStyle.DropDownList &&
                _CheckBoxComboBox.DataSource == null &&
                !DesignMode)
            {
                _CheckBoxComboBox.CheckBoxItems[0].Visible = false;
            }

            ResumeLayout();
        }