CheckedListBox用法

1.添加项
----------------方法一--------------
checkedListBox1.Items.Add("蓝色");
checkedListBox1.Items.Add("红色");
checkedListBox1.Items.Add("黄色");
----------------方法二--------------
checkedListBox1.DataSource = dtblTable;
checkedListBox1.ValueMember = "RoomName";
checkedListBox1.DisplayMember = "RoomName";

2. 判断第i项是否选中,选中为true,否则为false
if(checkedListBox1.GetItemChecked(i))
{
     return true;
}
else
{
     return false;
}

3. 设置第i项是否选中
checkedListBox1.SetItemChecked(i, true); //true改为false为没有选中。

4.设置全选
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
 checkedListBox1.SetItemChecked(i, true);
}

5.得到全部选中的值 ,并将选中的项的文本组合成为一个字符串。
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]);
           }
       }
}

6. 设置CheckedListBox中第i项的Checked状态
checkedListBox1.SetItemCheckState(i, CheckState.Checked);

7.获取绑定的Value值
checkedListBox1.SelectedValue.ToString();

8. 清除checkedListBox1中所有的选项

for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
    checkedListBox1.Items.Clear();
}

9. //反向选择checkedListBox1的选项
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
    if (checkedListBox1.GetItemChecked(i))
   {
       checkedListBox1.SetItemChecked(i, false);
   }
   else
   {
       checkedListBox1.SetItemChecked(i, true);
   }
}

转载于:https://www.cnblogs.com/zhangpengshou/archive/2010/06/12/1757314.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值