/// <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);
}
}
}
}
}

  测试:

        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() + ".");
}
}

  设置已选择项:

            //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);
}

  

csharp:A Custom CheckedListBox with Datasource的更多相关文章

  1. CheckedListBox与下拉框联动代码

    private void yewubind(string id) { //给业务类型下拉框绑定业务类型数据 DataTable dtyewu = sb.SelectLast(id, 0); bool ...

  2. K8s仪表盘

    { "__inputs": [ { "name": "DS_TEST-ENVIORMENT-K8S", "label": ...

  3. spring boot 四大组件之Auto Configuration

    SpringBoot 自动配置主要通过 @EnableAutoConfiguration, @Conditional, @EnableConfigurationProperties 或者 @Confi ...

  4. Advanced Collection Views and Building Custom Layouts

    Advanced Collection Views and Building Custom Layouts UICollectionView的结构回顾 首先回顾一下Collection View的构成 ...

  5. csharp: get Web.Services WebMethod

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  6. Winform开发常用控件之Checkbox和CheckedListBox

    Winform的开发基本都是基于控件事件的,也就是事件驱动型的. 多选框的放置和值的获取有很多种,这里介绍几个简单常用的方法 1.直接放置Checkbox,并获取Checkbox的值 上图 做法也非常 ...

  7. winform中的checkedListbox数据源绑定

    首先看清楚一点 winform下该控件的名称叫做:checkedListbox webform下叫做CheckBoxList 不知道这样起名的用意何在,这个别管了,看看用法吧. web下很简单,直接设 ...

  8. Sitecore Digital Marketing System, Part 1: Creating personalized, custom content for site visitors(自定义SiteCore中的 Item的Personalize的Condition) -摘自网络

    Sitecore’s Digital Marketing System (DMS) can help you personalize the content your site displays to ...

  9. CheckedListBox与CheckedListBox联动

    包括保存和加载 //查找业务类型 DataTable dtyewu = sb.SelectSyscode(0, true); if (dtyewu.Rows.Count > 0) { flagc ...

随机推荐

  1. 记录一次因为意外断电造成gitlab(docker容器)重启之后无法访问的问题

    容器启动成功,但是处于unhealthy状态,登录界面500. docker logs gitlab 最终错误是 err="opening storage failed: open bloc ...

  2. c语言求方阵的行列式、伴随矩阵算法

    #include<stdio.h> #include<math.h> #define N 100 //N比输入的阶数大即可 int main() {   int n,a[N][ ...

  3. iOS9下UICollectionViewCell的awakeFromNib问题

    最近项目测试出一个隐藏已久的bug,经过多番测试,发现在iOS9下自定义的一个UICollectionViewCell只走一次awakeFromNib. 具体情况是,项目中有一个控制器用到了自定义的U ...

  4. MySQL数据库的账户管理

    账户管理 在生产环境下操作数据库时,绝对不可以使用root账户连接,而是创建特定的账户,授予这个账户特定的操作权限,然后连接进行操作,主要的操作就是数据的crud MySQL账户体系:根据账户所具有的 ...

  5. Java 子类父类构造方法执行顺序

    public class Test { class Super { int flag = 1; Super() { test(); } void test() { System.out.println ...

  6. 二分难题 && deque

    141. Sqrt(x) https://www.lintcode.com/problem/sqrtx/description?_from=ladder&&fromId=4 publi ...

  7. 不好意思啊,我上周到今天不到10天时间,用纯C语言写了一个小站!想拍砖的就赶紧拿出来拍啊

    花10天时间用C语言做了个小站 http://tieba.yunxunmi.com/index.html 简称: 云贴吧 不好意思啊,我上周到今天不到10天时间,用纯C语言写了一个小站!想拍砖的就赶紧 ...

  8. java中的POJO、PO、VO分别是什么?

    1.PO:persistant object 持久对象 可以看成是与数据库中的表相映射的java对象.使用Hibernate来生成PO是不错的选择. 2. VO:value object值对象. 通常 ...

  9. thinkPHP5配置nginx环境无法打开(require(): open_basedir restriction in effect. File(/mnt/hgfs/root/tp5/thinkphp/start.php) is not within the allowed path(s)

    今天想把玩一下tp5,结果怎么都无法访问,每次都是报500错误,我把错误提示都打开看到下面的错误 require(): open_basedir restriction in effect. File ...

  10. java代理-cglib

    前面说到了java的动态代理,但是动态代理依赖于接口,这次来看看cglib来实现的代理... 假设有如下方法,这回没有说接口哦~ package proxy.cglibProxy; public cl ...