zoukankan      html  css  js  c++  java
  • CheckedComboBoxEdit实现单选

    一般地,我们用ComboBoxEdit来实现下拉单选,但它的item只能一个字符串,而不是一个自定义的对象。因此,我们常用CheckedComboBoxEdit来代替ComboBoxEdit,但CheckedComboBoxEdit默认是可以多选的,所以,我们要写一个单选处理事件。效果如下:

    自定义对象:

    private static DataTable CreateTable(int rowCount)
    {
        DataTable datatable = new DataTable();
        datatable.Columns.Add("Name", typeof(string));
        datatable.Columns.Add("ID", typeof(int));
        datatable.Columns.Add("Number", typeof(int));
        datatable.Columns.Add("Date", typeof(DateTime));
        for (int i = 0; i < rowCount; i++)
            datatable.Rows.Add(new object[] { $"选项 {i}", i, 3 - i, DateTime.Now.AddDays(i) });
        return datatable;
    }
    

    绑定数据:

    checkedComboBoxEdit1.Properties.DataSource = CreateTable(10);
    checkedComboBoxEdit1.Properties.DisplayMember = "Name";
    checkedComboBoxEdit1.Properties.ValueMember = "ID";
    checkedComboBoxEdit1.Properties.SelectAllItemVisible = false;
    

    弹出窗口事件:

    private void checkedComboBoxEdit1_Popup(object sender, EventArgs e)
    {
        CheckedListBoxControl checkedListBoxControl = (sender as IPopupControl)?.PopupWindow.Controls.OfType<PopupContainerControl>().First().Controls.OfType<CheckedListBoxControl>().First();
    
        if (checkedListBoxControl != null)
        {
            checkedListBoxControl.ItemCheck -= checkedListBoxControl_ItemCheck;
            checkedListBoxControl.ItemCheck += checkedListBoxControl_ItemCheck;
        }
    }
    
    private void checkedListBoxControl_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e)
    {
        if (e.State == CheckState.Checked)
        {
            CheckedListBoxControl list = sender as CheckedListBoxControl;
            List<CheckedListBoxItem> items = new List<CheckedListBoxItem>();
            foreach (int index in list.CheckedIndices)
            {
                if (index == e.Index) continue;
                items.Add(list.Items[index]);
            }
            foreach (CheckedListBoxItem item in items)
                item.CheckState = CheckState.Unchecked;
        }
    }

    作者:我也是个傻瓜
    出处:http://www.cnblogs.com/liweis/
    签名:成熟是一种明亮而不刺眼的光辉。

  • 相关阅读:
    ps_基于2020的官方教程
    杂记_好玩的
    linux _文件目录与权限
    levelDb笔记
    《好学的C++ 第2版》 第9章 一些高级编程技术
    《好学的C++ 第2版》 第8章 文件-电子存储
    《好学的C++ 第2版》 第7章 字符串--分析文本
    《好学的C++ 第2版》 第6章 指针--我知道数据在哪里
    《好学的C++ 第2版》 第5章 数组--都给我排好队
    《好学的C++ 第2版》 第4章 函数--分工与合作
  • 原文地址:https://www.cnblogs.com/liweis/p/14632093.html
Copyright © 2011-2022 走看看