csharp:A Custom CheckedListBox with Datasource

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
/// <summary>
   /// (eraghi)
   /// Custom CheckedListBox with binding facilities (Value property)
   /// from A Custom CheckedListBox with Datasource  http://www.codeproject.com/Articles/22960/A-Custom-CheckedListBox-with-Datasource-Implementa
   /// </summary>
   [ToolboxBitmap(typeof(CheckedListBox))]
   public class DuCheckedListBox : CheckedListBox
   {
       /// <summary>
       /// Default constructor
       /// </summary>
       public DuCheckedListBox()
       {
           this.CheckOnClick = true;
 
       }
 
 
 
       /// <summary>
       ///    Gets or sets the property to display for this CustomControls.CheckedListBox.
       ///
       /// Returns:
       ///     A System.String specifying the name of an object property that is contained
       ///     in the collection specified by the CustomControls.CheckedListBox.DataSource
       ///     property. The default is an empty string ("").
       /// </summary>
       [DefaultValue("")]
       [TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=3de65a4a-2b5f-4d9d-88de-bfb692b10f93")]
       [Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=3de65a4a-2b5f-4d9d-88de-bfb692b10f93", typeof(UITypeEditor))]
       [Browsable(true)]
       public new string DisplayMember
       {
           get
           {
               return base.DisplayMember;
           }
           set
           {
               base.DisplayMember = value;
 
           }
       }
 
       /// <summary>
       ///    Gets or sets the property to get the values for this CustomControls.CheckedListBox.
       ///
       /// Returns:
       ///     A System.String specifying the name of an object property that is contained
       ///     in the collection specified by the CustomControls.CheckedListBox.DataSource
       ///     property. The default is an empty string ("").
       /// </summary>
       [DefaultValue("")]
       [TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=3de65a4a-2b5f-4d9d-88de-bfb692b10f93")]
       [Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=3de65a4a-2b5f-4d9d-88de-bfb692b10f93", typeof(UITypeEditor))]
       [Browsable(true)]
       public new string ValueMember
       {
           get
           {
               return base.ValueMember;
           }
           set
           {
               base.ValueMember = value;
 
           }
       }
 
 
       /// <summary>
       /// Gets or sets the data source for this CustomControls.CheckedListBox.
       /// Returns:
       ///    An object that implements the System.Collections.IList or System.ComponentModel.IListSource
       ///    interfaces, such as a System.Data.DataSet or an System.Array. The default
       ///    is null.
       ///
       ///Exceptions:
       ///  System.ArgumentException:
       ///    The assigned value does not implement the System.Collections.IList or System.ComponentModel.IListSource
       ///    interfaces.
       /// </summary>
       [DefaultValue("")]
       [AttributeProvider(typeof(IListSource))]
       [RefreshProperties(RefreshProperties.All)]
       [Browsable(true)]
       public new object DataSource
       {
           get
           {
               return base.DataSource;
           }
           set
           {
               base.DataSource = value;
 
           }
       }
 
       /// <summary>
       /// Gets and sets an integer array of the values based on checked items values ID
       /// </summary>
       [Bindable(true), Browsable(true)]
       public List<int> ValueList
       {
           get
           {
               ///Gets checked items id values in a list
               List<int> retArray = new List<int>();
               PropertyDescriptor prop = null;
               PropertyDescriptorCollection propList = this.DataManager.GetItemProperties();
               prop = propList.Find(this.ValueMember, false);
               object checkedItem;
               if (prop != null)
               {
                   for (int i = 0; i < this.Items.Count; i++)
                   {
                       if (this.GetItemChecked(i))
                       {
                           checkedItem = this.DataManager.List[i];
                           retArray.Add(Convert.ToInt32(prop.GetValue(checkedItem).ToString()));
                       }
                   }
               }
               return retArray;
           }
 
           set
           {
               ///Sets checked items base on id values in a list
               List<int> myList = value;
               PropertyDescriptor prop = null;
               PropertyDescriptorCollection propList = this.DataManager.GetItemProperties();
               prop = propList.Find(this.ValueMember, false);
               object checkedItem;
               int intValItem;
               int found;
               if (prop != null)
               {
                   for (int i = 0; i < this.Items.Count; i++)
                   {
                       checkedItem = this.DataManager.List[i];
                       intValItem = Convert.ToInt32(prop.GetValue(checkedItem).ToString());
                       found = (from c in myList where c == intValItem select c).Count();
                       if (found == 1)
                           this.SetItemCheckState(i, CheckState.Checked);
                       else
                           this.SetItemCheckState(i, CheckState.Unchecked);
                   }
               }
           }
       }
   }

  测试:

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
DataTable setData()
{
    DataTable dt = new DataTable();
    dt.Columns.Add("ID", typeof(int));
    dt.Columns.Add("Name", typeof(string));
    dt.Rows.Add(1, "涂聚文");
    dt.Rows.Add(2, "Geovin Du");
    dt.Rows.Add(3, "geovindu");
    dt.Rows.Add(4, "涂鸦王国");
    dt.Rows.Add(5, "涂氏");
    dt.Rows.Add(6, "张氏");
    dt.Rows.Add(7, "郭氏");
    dt.Rows.Add(8, "江氏");
    return dt;
}
 
/// <summary>
///
/// </summary>
public CheckedlistboxForm()
{
    InitializeComponent();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CheckedlistboxForm_Load(object sender, EventArgs e)
{
    this.duCheckedListBox1.DataSource = setData();
    this.duCheckedListBox1.DisplayMember = "Name";
    this.duCheckedListBox1.ValueMember = "ID";
    //设定
    bool  insideCheckEveryOther = true;
    for (int i = 0; i < duCheckedListBox1.Items.Count; i++)
    {
        // For every other item in the list, set as checked.
        if ((i % 2) == 0)
        {
            // But for each other item that is to be checked, set as being in an
            // indeterminate checked state.
            if ((i % 4) == 0)
                duCheckedListBox1.SetItemCheckState(i, CheckState.Indeterminate);
            else
                duCheckedListBox1.SetItemChecked(i, true);
        }
    }
    insideCheckEveryOther = false;
 
 
 
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
 
    IEnumerator myEnumerator;
    myEnumerator = duCheckedListBox1.CheckedIndices.GetEnumerator();
    int y;
    //选择为全为无选
    //while (myEnumerator.MoveNext() != false)
    //{
    //    y = (int)myEnumerator.Current;
    //    duCheckedListBox1.SetItemChecked(y, false);
    //}
 
    //foreach (object itemChecked in duCheckedListBox1.CheckedItems)
    //{
    //    MessageBox.Show("Item with title: \"" + itemChecked.ToString() +
    //            "\", is checked. Checked state is: " +
    //            duCheckedListBox1.GetItemCheckState(duCheckedListBox1.Items.IndexOf(itemChecked)).ToString() + ".");
    //}
 
    foreach (DataRowView itemChecked in duCheckedListBox1.CheckedItems)
    {
        MessageBox.Show("Item with title: \"" + itemChecked[0].ToString() + itemChecked[1].ToString() +
                "\", is checked. Checked state is: " +
                duCheckedListBox1.GetItemCheckState(duCheckedListBox1.Items.IndexOf(itemChecked)).ToString() + ".");
    }
}

  设置已选择项:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//2.
List<int> list;
list = new List<int> { 1, 4 };
int value = 0;
//for (int i = 0; i < duCheckedListBox1.Items.Count; i++)
//{
//    DataRowView view = duCheckedListBox1.Items[i] as DataRowView;
//    value = (int)view["ID"];
//    if (list.Contains(value))
//        duCheckedListBox1.SetItemChecked(i, true);
//}
for (int i = 0; i < duCheckedListBox1.Items.Count; i++)
{
    DataRowView view = duCheckedListBox1.Items[i] as DataRowView;
    value = (int)view["ID"];
    if (value == 5)
        duCheckedListBox1.SetItemChecked(i, true);
}

  

posted @   ®Geovin Du Dream Park™  阅读(453)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2011-05-09 C# 获取源代码
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示