1
yhj
2024-07-24 5e5d945e91568b973faa27d8ab0bcef99fc4a6c5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#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
{
    /// <summary>
    ///     支持输入的CheckedListBox控件.
    /// </summary>
    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;
        }
    }
}