博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DEV CheckComboboxEdit、CheckedListBoxControl(转)
阅读量:5999 次
发布时间:2019-06-20

本文共 4233 字,大约阅读时间需要 14 分钟。

CheckComboboxEdit

   //先清空所有,若在窗体Load事件中,也可以不清空

            //cbRWYs.Properties.Items.Clear();

            var RwyList = tspro.Airport.Rwydirs.Select(c=>new { c.RUNWAY_DIRECTION_UUID, c.DESIGNATOR }).ToList(); //设置值列表           

            //combobox赋值

            CheckedListBoxItem[] itemListQuery = new CheckedListBoxItem[RwyList.Count];

            for (int i = 0; i < RwyList.Count; i++)

            {

                itemListQuery[i] = new CheckedListBoxItem(RwyList[i].RUNWAY_DIRECTION_UUID, RwyList[i].DESIGNATOR);

            }

            cbRWYs.Properties.Items.AddRange(itemListQuery);

 

            //cbRWYs.Properties.ShowPopupCloseButton = false; //是否显示Cancel按钮

    //cbRWYs.Properties.ShowButtons = false;//是否显示Ok Cancel按钮

            //cbRWYs.Properties.SelectAllItemVisible = false; //不显示Select All选项

            cbRWYs.Properties.SelectAllItemCaption = "全选"; //将Select All 选择改名

   //目前没找到更改Ok,Cancel按钮的方法,可能不支持改这两个按钮

 

   //赋值

   foreach (CheckedListBoxItem airportItem in this.cbxServedAirport.Properties.Items)

            {
                if (CmmUtils.GetString(airportItem.Value) == despTSData.AIRPORT_NAME)
                {
                    airportItem.CheckState = CheckState.Checked;
                }
                else
                {
                    airportItem.CheckState = CheckState.Unchecked;
                }
            }

//全选用CheckAll

 

         

         

//清空项

            checkedComboBoxEdit1.Properties.Items.Clear();

 

            //自定义数组

            string[] strs=new string[]{"新建","审批中","已完成","已撤销"};
            //添加项
            checkedComboBoxEdit1.Properties.Items.AddRange(strs);

 

            //设置选中状态

            if(checkedComboBoxEdit1.Properties.Items.Count>0){
                //设置选中状态
                checkedComboBoxEdit1.Properties.Items[strs[0]].CheckState = CheckState.Checked;
                //设置选项是否可用
                checkedComboBoxEdit1.Properties.Items[strs[0]].Enabled = false;
            }
            //取值
            checkedComboBoxEdit1.EditValue.ToString();
            //获取各项值 放在List集合中
            List<object> List = checkedComboBoxEdit1.Properties.Items.GetCheckedValues();

 

            //注意 当取得值是多项时,各项之间的间隔是 英文状态下 逗号+空格

            //转换方法
            string result = checkedComboBoxEdit1.EditValue.ToString().Replace(", ", ",");

 

            //是否显示 确定、取消按钮

            checkedComboBoxEdit1.Properties.ShowButtons = false;
            //是否显示 取消按钮
            checkedComboBoxEdit1.Properties.ShowPopupCloseButton = false;

 

            //下拉显示项的个数 (设置为下拉个数加1正好可以显示全部,因为有一行是全选项)

            checkedComboBoxEdit1.Properties.DropDownRows = checkedComboBoxEdit1.Properties.Items.Count + 1;

 

CheckedListBoxControl

        

         

//自定义一个表

            DataTable dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("Name");
            dt.Columns.Add("Sex");
            for (int i = 0; i < 30; i++) {
                DataRow dr = dt.NewRow();
                dr["ID"] = i + 1;
                dr["Name"]=Convert.ToString((char)(65+i))+Convert.ToString((char)(65+i));
                dr["Sex"] = i % 2==0?"男":"女";
                dt.Rows.Add(dr);
            }
            //清空项
            checkedListBoxControl1.Items.Clear();

 

            //绑定

            checkedListBoxControl1.DataSource = dt;
            checkedListBoxControl1.ValueMember = "ID";
            checkedListBoxControl1.DisplayMember = "Name";

 

            //全选

            //checkedListBoxControl1.CheckAll();

 

            //项的个数

            int itemCount = checkedListBoxControl1.ItemCount;

 

            //添加项(如果设置绑定,添加项无效)

            checkedListBoxControl1.Items.Add("kk");

 

            //设置选中状态、显示值、实际值、是否可用(如果设置绑定,这些将会无效)

            checkedListBoxControl1.Items[0].CheckState = CheckState.Checked;
            checkedListBoxControl1.Items[0].Description = "显示值";
            checkedListBoxControl1.Items[0].Value = "实际值";
            checkedListBoxControl1.Items[0].Enabled = false;
            //效果和上面一样
            checkedListBoxControl1.SetItemChecked(0, true);
            checkedListBoxControl1.SetItemCheckState(0, CheckState.Checked);
            checkedListBoxControl1.SetItemValue("实际值",0);           

 

            //是否被勾选

           bool isChecked=  checkedListBoxControl1.GetItemChecked(0);
            //获取某项状态
           string checkState = checkedListBoxControl1.GetItemCheckState(0).ToString();
            //获取某项绑定值 valueMember
           string trueValue = checkedListBoxControl1.GetItemValue(0).ToString();
            //获取某项显示值   displayMember
           string disValue = checkedListBoxControl1.GetDisplayItemValue(0).ToString();
           string disValue2 = checkedListBoxControl1.GetItemText(0);

 

            //是否点击一次 就改变状态

           checkedListBoxControl1.CheckOnClick = true;

 

            //是否多列显示

           checkedListBoxControl1.MultiColumn = true;

 

            //checkedListboxControl 是否获得焦点

           bool isfocus=checkedListBoxControl1.ContainsFocus;

 

           //实现单选功能

            checkedListBoxControl1.SelectedIndexChanged += new EventHandler(checkedListBoxControl1_SelectedIndexChanged);
         
            //获取选中项的绑定值(前提:手动添加的可以获取,但是datatable绑定的无法获取)
           List<object> objList = checkedListBoxControl1.Items.GetCheckedValues();

 

       void checkedListBoxControl1_SelectedIndexChanged(object sender, EventArgs e)

        {
            int index=checkedListBoxControl1.SelectedIndex;
            for (int i = 0; i < checkedListBoxControl1.ItemCount; i++) {
                if (i != index)
                {
                    checkedListBoxControl1.SetItemChecked(i, false);
                }
            }
        }

 

转载于:https://www.cnblogs.com/mol1995/p/7989758.html

你可能感兴趣的文章
PHP_SELF变量解析和重复路径解决
查看>>
git 命令行使用(基础篇)
查看>>
在VUE中利用MQTT协议实现即时通讯
查看>>
Data Lake Analytics: 读/写PolarDB的数据
查看>>
nuxt 简单介绍
查看>>
java Script 的if判断2
查看>>
什么是字面值
查看>>
在react-router中进行代码拆分
查看>>
OKR与Scrum如何强强联手
查看>>
猴子数据域名防封接口降低小说被封的风险
查看>>
138. Copy List with Random Pointer
查看>>
android:Activity启动模式
查看>>
你的第一个 Dojo 应用程序
查看>>
彻底搞清楚浏览器渲染过程
查看>>
搜索前端技术概述
查看>>
被BAT疯抢的工程师,都是怎么拿到50万年薪Offer的?
查看>>
Weex系列(6) —— web组件和webview
查看>>
在项目中遇到的一些bug
查看>>
Vue笔记(五)——Token&生命周期
查看>>
《前端十年心路-我把一切告诉你》的书稿大纲&问题收集
查看>>