ChecklistBox的使用总结

来源:互联网 发布:王者荣耀数据封神榜67 编辑:程序博客网 时间:2024/05/16 14:46

前言

  ChecklistBox看起来一个很简单的控件,使用的时候想要达到理想的效果是需要费一番功夫的,这篇博客对于近一两周来项目中主界面设计用到的ChecklistBox方法做了一点小小的总结。

图示

这里写图片描述

常用方法

  • 1.添加项
    checkedListBox1.Items.Add("吃饭");     checkedListBox1.Items.Add("睡觉");     checkedListBox1.Items.Add("打豆豆");
  • 2.设置复选框为单选
       private void ChecklistBox_ItemCheck(object sender, ItemCheckEventArgs e)        {                  if (ChecklistBox.CheckedItems.Count > 0)            {                for (int i = 0; i < ChecklistBox.Items.Count; i++)                {                    if (i != e.Index)                    {                       ChecklistBox.SetItemChecked(i, false);                    }                }            }           }
  • 3.设置整个复选框不可用,置灰
 ChecklistBox.Enabled = false;
  • 4.判断是否有选中的item
        //4.1 判断是否有选中项            if (ChecklistBox1.CheckedItems.Count == 0 )            {                MessageBox.Show("请先选中要执行的计划");            }        //4.2 判断第i项是否选中 ,选中返回true,否则选false            if(checkedListBox1.GetItemChecked(i))        {             return true;        }         else        {             return false;         }
  • 5.设置第i项的选中状态
      //true选中,false为未选中      checkedListBox1.SetItemChecked(i, true);  
  • 6.得到选中项的值
     //6.1得到一个选中项的值     string strPlan = checkedListBox1.SelectedItem.ToString();     //6.2得到所有选中项的值     string strCollected = string.Empty;     for (int i = 0; i < checkedListBox1.Items.Count; i++)     {          if (checkedListBox1.GetItemChecked(i))          {              if (strCollected == string.Empty)              {                   strCollected = checkedListBox1.GetItemText(checkedListBox1.Items[i]);              }              else              {                   strCollected = strCollected + "/" + checkedListBox1. GetItemText(checkedListBox1.Items[i]);               }           }    }
  • 7.清除复选框中所有item
    for (int i = 0; i < checkedListBox1.Items.Count; i++)    {            checkedListBox1.Items.Clear();    }
  • 8.反向选择复选框中的item
    for (int i = 0; i < checkedListBox1.Items.Count; i++)         {             if (checkedListBox1.GetItemChecked(i))            {                checkedListBox1.SetItemChecked(i, false);            }            else            {                checkedListBox1.SetItemChecked(i, true);            }         }   
  • 9.将item从一个复选框转移到另一个复选框
        for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++)         {                      checkedListBox2.Items.Add(this.checkedListBox1.CheckedItems);          //remove是除去一个具体的值,不是index             this.checkedListBox1.Items.Remove(this.checkedListBox1.CheckedItems);               }
  • 10.设置选中item置灰,不能重复再选
                for (int i = 0; i < clbOver.Items.Count; i++)                  {                clbOver.GetItemChecked(i);                clbOver.SetItemCheckState(i, CheckState.Indeterminate);                                            }

总结

 以上是最近一段时间对ChecklistBox实践过程中常用方法的总结,希望能对大家有所帮助。有一点感受是:在使用一个控件之前查看官方文档遍历一遍它的所有属性,方法和事件很有必要,有了这样一层宏观之后可以帮助大家在具体实践过程中少走弯路。