csharp:A Custom CheckedListBox with Datasource

简介:  from A Custom CheckedListBox with Datasource  http://www.codeproject.com/Articles/22960/A-Custom-CheckedListBox-with-Datasource-Implementa /// <summary> /// (eraghi) /// Cust


from A Custom CheckedListBox with Datasource  http://www.codeproject.com/Articles/22960/A-Custom-CheckedListBox-with-Datasource-Implementa

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



目录
相关文章
|
自然语言处理 安全 数据挖掘
大语言模型在假新闻的检测
大语言模型在假新闻检测应用中发挥重要作用。通过学习大量语言数据和模式,模型可以理解文本的语义和上下文信息,判断其真实性。模型通过监督学习训练,提取特征并预测新闻真实性。结合其他技术手段和人工审核,可以提高准确性和可信度。假新闻检测的过程包括数据准备、特征提取、模型训练和实际应用。模型在谣言检测中也有类似应用。
782 0
|
NoSQL Redis 数据安全/隐私保护
Redis 6.0 新特性详解
艺术致敬! 一、众多新模块(modules)API   Redis 6中模块API开发进展非常大,因为Redis Labs为了开发复杂的功能,从一开始就用上Redis模块。Redis可以变成一个框架,利用Modules来构建不同系统,而不需要从头开始写然后还要BSD许可。
9785 0
|
人工智能 IDE 测试技术
利用AI技术提升编程效率
【10月更文挑战第6天】本文将探讨如何通过人工智能(AI)技术提升编程效率。我们将介绍一些实用的工具和策略,如代码补全、错误检测和自动化测试,以及如何将这些工具整合到你的日常工作流程中。无论你是初学者还是经验丰富的开发者,都可以从这些技巧中受益。让我们一起探索如何利用AI技术来简化编程过程,提高生产力吧!
|
11月前
|
安全 Java API
Nacos 3.0 Alpha 发布,在安全、泛用、云原生更进一步
近期,我们欣喜地宣布 Nacos 3.0 的第一个版本 Nacos 3.0-ALPHA 已经发布。Nacos 3.0 的目标是在 2.0 的基础上,进一步优化安全性、易用性和标准化。同时,我们将引入更多功能,帮助用户在分布式协调、AI 大模型、云原生等多种场景中更好地使用 Nacos,以提升其广泛适应性。
491 129
|
12月前
|
机器学习/深度学习 编解码 异构计算
4090笔记本0.37秒直出大片!英伟达联手MIT清华祭出Sana架构,速度秒杀FLUX
英伟达、麻省理工学院与清华大学联合发布Sana,一款高效文本到图像生成框架。Sana通过深度压缩自编码器和线性注意力机制,实现快速高分辨率图像生成,生成1024×1024图像仅需不到1秒。此外,Sana采用解码器专用文本编码器增强文本与图像对齐度,大幅提高生成质量和效率。相比现有模型,Sana体积更小、速度更快,适用于多种设备。
241 7
|
人工智能 搜索推荐 区块链
元宇宙:虚拟现实的新纪元
在科技飞速发展的今天,元宇宙(Metaverse)正从科幻走向现实,成为连接物理与数字世界的桥梁。本文将探讨元宇宙的定义、关键技术、现有形态及未来展望,带您领略这一虚拟空间的无限可能。元宇宙利用VR、AR、区块链等技术,构建了一个沉浸式的虚拟世界,用户可通过数字化身自由探索、社交、娱乐、学习和工作。尽管仍处初级阶段,但虚拟会议、数字艺术、游戏娱乐等领域已初现端倪。未来,元宇宙有望成为第二人生、数字商务、创新教育和远程协作的重要平台,同时也面临数据安全、隐私保护等挑战。
1031 1
|
SQL 关系型数据库 HIVE
KLOOK客路旅行基于Apache Hudi的数据湖实践
KLOOK客路旅行基于Apache Hudi的数据湖实践
336 2
KLOOK客路旅行基于Apache Hudi的数据湖实践
|
Windows
最新永久汉化免费版PS2023Photoshop2023版本ACR15
Photoshop 2023 v24.0.1.547是由Adobe公司最新推出的高效、专业、实用的图像处理软件,同时该软件主要是以其强悍的编辑和调整、绘图等功能得到广泛的应用,其中还有各种图片的调整和图画绘制以及图像的修复、调色等一系列的工具都是数不胜数,使用范围也是非常的广,我们从照片修饰到海报、包装、横幅的制作,再到照片的处理,只要您需要我们就可以做到,丰富的预设让用户的工作可以更加的轻松。
1557 0
|
NoSQL Linux Redis
【redis】windows安装redis
【redis】windows安装redis
366 0
|
存储 关系型数据库 MySQL
Mysql数据库设计规范和技巧
Mysql数据库设计规范和技巧

热门文章

最新文章