/// <summary> /// Builds a CSV string of the items selected. /// </summary> internal string GetCSVText(bool skipFirstItem) { string ListText = String.Empty; int StartIndex = DropDownStyle == ComboBoxStyle.DropDownList && DataSource == null && skipFirstItem ? 1 : 0; for (int Index = StartIndex; Index <= _CheckBoxComboBoxListControl.Items.Count - 1; Index++) { CheckBoxComboBoxItem Item = _CheckBoxComboBoxListControl.Items[Index]; if (Item.Checked) { ListText += string.IsNullOrEmpty(ListText) ? Item.Text : String.Format(", {0}", Item.Text); } } return(ListText); }
/// <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; var 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.Font = _CheckBoxComboBox.Font; 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(); }