checkedListBox 扩展

本文介绍了一个使用C#实现的列表绑定示例,通过创建Person类并填充数据,然后将其绑定到界面元素上,展示了如何在界面上显示列表数据及响应选择变化。

        private void MainForm_Load(object sender, EventArgs e)
        {
            BuldList();

            List<Person> lst = new List<Person>();
            Person p1 = new Person();
            p1.Id = 1;
            p1.Name = "张三";
            p1.Sex = "先生";
            lst.Add(p1);

            Person p2 = new Person();
            p2.Id = 2;
            p2.Name = "李四";
            p2.Sex = "人妖";
            lst.Add(p2);

            //注意:这三个属性为隐藏属性,不会在写代码时提示出来,但能用
            this.checkedListBox1.DataSource = lst;
            this.checkedListBox1.DisplayMember = "Name";
            this.checkedListBox1.ValueMember = "Id";
        }

        public class Person
        {
            private string m_name;
            public string Name
            {
                get { return m_name; }
                set { m_name = value; }
            }
            private int m_id;
            public int Id
            {
                get { return m_id; }
                set { m_id = value; }
            }
            private string m_sex;
            public string Sex
            {
                get { return m_sex; }
                set { m_sex = value; }
            }
        }

        private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            Person p = this.checkedListBox1.Items[e.Index] as Person;
            if (p != null)
            {
                MessageBox.Show("姓名:" + p.Name + " ID:" + p.Id + " Sex:" + p.Sex);
            }
        }

在 WinForms 中,可以通过 `CheckedListBox` 结合 `Panel` 和 `TextBox` 模拟一个多选下拉框。以下是完整实现步骤和代码示例: --- ### **实现步骤** 1. **界面布局** - 添加一个 `TextBox`(用于显示选中项)和一个 `Button`(触发下拉)。 - 添加一个 `Panel`(包含 `CheckedListBox`),默认隐藏。 2. **逻辑处理** - 点击按钮时显示/隐藏 `Panel`。 - 用户勾选 `CheckedListBox` 中的项后,更新 `TextBox` 的显示内容。 - 点击其他区域时隐藏 `Panel`(需处理 `LostFocus` 事件)。 --- ### **代码示例** ```csharp using System; using System.Linq; using System.Windows.Forms; public class MultiSelectComboBox : Form { private TextBox textBox; private Button dropDownButton; private Panel dropDownPanel; private CheckedListBox checkedListBox; public MultiSelectComboBox() { // 初始化控件 textBox = new TextBox { ReadOnly = true, Location = new System.Drawing.Point(10, 10), Width = 200 }; dropDownButton = new Button { Text = "▼", Location = new System.Drawing.Point(210, 10), Width = 30 }; dropDownPanel = new Panel { Location = new System.Drawing.Point(10, 35), Width = 230, Height = 150, BorderStyle = BorderStyle.FixedSingle, Visible = false }; checkedListBox = new CheckedListBox { Dock = DockStyle.Fill }; // 添加选项 checkedListBox.Items.AddRange(new object[] { "选项1", "选项2", "选项3", "选项4" }); // 事件绑定 dropDownButton.Click += (s, e) => ToggleDropDown(); checkedListBox.ItemCheck += (s, e) => UpdateSelectedItems(); textBox.Click += (s, e) => ToggleDropDown(); this.Click += (s, e) => { if (!dropDownPanel.Bounds.Contains(Cursor.Position)) HideDropDown(); }; // 添加到窗体 dropDownPanel.Controls.Add(checkedListBox); Controls.AddRange(new Control[] { textBox, dropDownButton, dropDownPanel }); } private void ToggleDropDown() { dropDownPanel.Visible = !dropDownPanel.Visible; } private void HideDropDown() { dropDownPanel.Visible = false; } private void UpdateSelectedItems() { var selectedItems = checkedListBox.CheckedItems.Cast<string>(); textBox.Text = string.Join(", ", selectedItems); } [STAThread] public static void Main() { Application.EnableVisualStyles(); Application.Run(new MultiSelectComboBox()); } } ``` --- ### **关键点说明** 1. **`CheckedListBox` 的选中项同步** 通过 `ItemCheck` 事件实时更新 `TextBox` 的显示内容。 2. **下拉面板的显示/隐藏** - 点击按钮或 `TextBox` 时切换 `Panel` 的可见性。 - 点击窗体其他区域时隐藏 `Panel`(通过 `Bounds.Contains` 判断)。 3. **样式优化** - 可调整 `Panel` 的 `BorderStyle` 模拟下拉框边框。 - 通过 `DockStyle.Fill` 让 `CheckedListBox` 填满 `Panel`。 --- ### **效果图描述** - 默认显示一个 `TextBox` 和下拉按钮。 - 点击后弹出 `Panel`,内含可勾选的 `CheckedListBox`。 - 选中项会实时显示在 `TextBox` 中,用逗号分隔。 --- ### **扩展功能** - **搜索过滤**:在 `TextBox` 中输入时动态过滤 `CheckedListBox` 的选项。 - **数据绑定**:将 `CheckedListBox` 的数据源绑定到数据库或集合。 - **自定义样式**:修改选中项的背景色或勾选框图标。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值