#region using System; using System.ComponentModel; using System.Data; using System.Windows.Forms; using CSFrameworkV5.Core; using DevExpress.XtraEditors.Controls; using ItemCheckEventArgs = DevExpress.XtraEditors.Controls.ItemCheckEventArgs; #endregion namespace CSFrameworkV5.Library.UserControls { /// /// 支持输入的CheckedListBox控件. /// public partial class ucCheckedListBoxEditor : UserControl { private string _EditValue = ""; private EventHandler _OnCheckStateChanged; private string _Seperator = ","; public ucCheckedListBoxEditor() { InitializeComponent(); } [Description("返回列表中已勾选的值,以分隔符隔开")] public string EditValue { get { _EditValue = GetItemCheckedValue(); return _EditValue; } set { _EditValue = value; SetItemChecked(value); } } [DefaultValue(",")] [Description("多个值中间的分隔符.如:AA,BB,CC")] public string ItemSeperator { get => _Seperator; set => _Seperator = value; } public override string Text { get => EditValue; set => EditValue = value; } public void BindingDataSource(string[] items) { foreach (var item in items) lists.Items.Add(item, item, CheckState.Unchecked, true); } public void BindingDataSource(DataTable dataSource, string valueMember, string displayMember) { foreach (DataRow row in dataSource.Rows) lists.Items.Add(row[valueMember], row[displayMember].ToStringEx(), CheckState.Unchecked, true); } private void btnAdd_Click(object sender, EventArgs e) { if (txtNewItem.Text != string.Empty) { var index = lists.Items.Add(txtNewItem.Text, txtNewItem.Text, CheckState.Checked, true); lists.SelectedIndex = index; popupEdit.EditValue = EditValue; txtNewItem.Text = ""; txtNewItem.Focus(); } } private string GetItemCheckedValue() { var result = ""; foreach (CheckedListBoxItem item in lists.Items) if (item.CheckState == CheckState.Checked) result = result + _Seperator + item.Value; return result == "" ? "" : result + _Seperator; } private void lists_ItemCheck(object sender, ItemCheckEventArgs e) { popupEdit.EditValue = EditValue; if (_OnCheckStateChanged != null) _OnCheckStateChanged(lists, e); } private void lists_SelectedValueChanged(object sender, EventArgs e) { if (_OnCheckStateChanged != null) _OnCheckStateChanged(lists, e); } public event EventHandler OnCheckStateChanged { add => _OnCheckStateChanged += value; remove => _OnCheckStateChanged -= value; } private void popupContainerControl1_SizeChanged(object sender, EventArgs e) { txtNewItem.Width = popupContainerControl1.Width - btnAdd.Width - 16; } private void popupEdit_QueryPopUp(object sender, CancelEventArgs e) { if (popupContainerControl1.Width < Width) popupContainerControl1.Width = Width; } private void SetItemChecked(string value) { var items = value.Split(new[] { _Seperator }, StringSplitOptions.None); foreach (CheckedListBoxItem item in lists.Items) foreach (var s in items) if (s.ToLower() == item.Value.ToStringEx().ToLower()) { item.CheckState = CheckState.Checked; break; } } private void ucCheckedListBoxEditor_Load(object sender, EventArgs e) { Height = 21; popupEdit.Top = 0; popupEdit.Left = 0; // popupEdit.Width = this.Width; } private void ucCheckedListBoxEditor_SizeChanged(object sender, EventArgs e) { popupEdit.Width = Width; } } }