Winform中checklistbox控件的常用方法
最近用到checklistbox控件,在使用其过程中,收集了其相关的代码段
1.
添加项
checkedListBox1.Items.Add("蓝色"); 
checkedListBox1.Items.Add("红色"); 
checkedListBox1.Items.Add("黄色");

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

else
{
return false; 
}

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

4. 
设置全选 
添加一个名为select_all的checkbox控件,由其控制checkedListBox是全选还是全不选。
private void select_all_CheckedChanged(object sender, EventArgs e) 

if(select_all.Checked) 
{
for (int j = 0; j < checkedListBox1.Items.Count; j++) 
checkedListBox1.SetItemChecked(j, true); 
}
else 
{
for (int j =0; j < checkedListBox1.Items.Count; j++) 
checkedListBox1.SetItemChecked(j, false);
}
}

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. 
private void checkBoxAll_CheckedChanged(object sender, EventArgs e) 

if (checkBoxAll.Checked) 

//被选择了则将CheckedListBox中的所有条目都变为Checked状态 
for (int i = 0; i < checkedListBoxLayerControl.Items.Count;
i++) 

checkedListBoxLayerControl.SetItemCheckState(i, 
CheckState.Checked); 

}
else 

//否则变成Unchecked状态 
for (int i = 0;
i < checkedListBoxLayerControl.Items.Count; i++) 
{
checkedListBoxLayerControl.SetItemCheckState(i, CheckState.Unchecked); 

}
}
8. 
checkedListBox 单选设置(代码实现)
private void chkl_ItemAuditing_ItemCheck(object sender, 
ItemCheckEventArgs e)

if (chkl_ItemAuditing.CheckedItems.Count > 0) 

for (int i = 0; i < chkl_ItemAuditing.Items.Count; i++) 
{
if (i != e.Index) 

this.chkl_ItemAuditing.SetItemCheckState(i, 
System.Windows.Forms.CheckState.Unchecked); 



}
9. 
checkedListBox1显示一个数据库中关键字对应的所有记录
for (int i = 0; i < table.Rows.Count; i++) 

string name = table.Rows["myname"].ToString(); 
string paw = table.Rows["mypaw"].ToString(); 
checkedListBox1.Items.Add(name + paw); 
}

10. 
for(i=0;i<CheckedListBox.Items.Count;i++) 

if(CheckedListBox.GetItemText(
CheckedListBox.Items)=="你得到的值") 

CheckedListBox.SetItemChecked(i,true); 

}

11. 
清除checkedListBox1中所有的选项
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.Items.Clear();
}

12. 
//设置索引为index的项为选中状态
for (int i = 0; i < checkedListBox1.Items.Count; i++) 
{
checkedListBox1.SetItemChecked(i, true);


13. 
for (int i = 0; i < checkedListBox1.Items.Count; i++) 
{
if (checkedListBox1.GetSelected(i)) 
{
MessageBox.Show(checkedListBox1.CheckedItems.ToString());
}
}

14.
//选中checkedListBox1所有的选项

for (int i = 0; i < checkedListBox1.Items.Count; i++) 
{
checkedListBox1.SetItemCheckState(i, CheckState.Checked);
}

15. 
for (int i = 0; i < checkedListBox1.Items.Count; i++) 

//如果checkedListBox1的第i项被选中,
//则显示checkedListBox1对应的值
if (checkedListBox1.GetItemChecked(i)) 

MessageBox.Show(checkedListBox1.Items.ToString()); 
}
}

16. 
//反向选择checkedListBox1的选项
for (int i = 0; i < checkedListBox1.Items.Count; i++) 

if (checkedListBox1.GetItemChecked(i)) 

checkedListBox1.SetItemChecked(i, false); 

else 

checkedListBox1.SetItemChecked(i, true); 

}
17. 
//checkedListBox1中选定的项->checkedListBox2
for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++) 

checkedListBox2.Items.Add(this.checkedListBox1.CheckedItems);

//remove是除去一个具体的值,不是index,注意了
this.checkedListBox1.Items.Remove(
this.checkedListBox1.CheckedItems); 
}

Winform中checklistbox控件的常用方法的更多相关文章

  1. [C#]WinForm 中 comboBox控件之数据绑定

    [C#]WinForm 中 comboBox控件之数据绑定 一.IList 现在我们直接创建一个List集合,然后绑定 IList<string> list = new List<s ...

  2. Winform中Treeview控件失去焦点,将选择的节点设置为高亮显示 (2012-07-16 13:47:07)转载▼

    Winform中Treeview控件失去焦点,将选择的节点设置为高亮显示 (2012-07-16 13:47:07)转载▼标签: winform treeview drawnode Treeview控 ...

  3. C# WinForm中 让控件全屏显示的实现代码

    夏荣全 ( lyout(at)163.com )原文 C#中让控件全屏显示的实现代码(WinForm) 有时候需要让窗口中某一块的内容全屏显示,比如视频播放.地图等等.经过摸索,暂时发现两种可行方法, ...

  4. Winform中TextBox控件开启自动提示补全功能

    问题:Winform开发中,有一个TextBox控件用以输入姓名,现希望在输入名字时能够自动提示所有可能的名字. 解答:winform中的TextBox控件含有如下三个属性:   ① AutoComp ...

  5. Winform中Picture控件图片的拖拽显示

    注解:最近做了一个小工具,在Winform中对Picture控件有一个需求,可以通过鼠标从外部拖拽图片到控件的上,释放鼠标,显示图片! 首先你需要对你的整个Fom窗口的AllowDrop设置Ture ...

  6. winform中comboBox控件加默认选项的问题

    winform程序设计中,label,TextBox,ComboBox等几个控件几乎是用得最多的,在设计中经常会遇到一些小问题,如:comboBox控件绑定了数据源之后,如何设置默认值? combob ...

  7. c#WinForm中TeeChart控件的注册和使用

    首先要注册好TeeChart控件,注册方法参考:https://blog.csdn.net/my_clear_mind/article/details/79741020 完成注册之后,新建一个WinF ...

  8. 通过同步上下文方式更新winform中的控件信息

    SynchronizationContext 类是一个基类,可提供不带同步的自由线程上下文. 此类实现的同步模型的目的是使公共语言运行库内部的异步/同步操作能够针对不同的异步模型采取正确的行为.此模型 ...

  9. Winform 中DataGridView控件添加行标题

    有很多种方法. 1.可以在DataGridView控件中的RowStateChanged事件改变行标题单元格的值(Row.HeaderCell.Value) /// <summary> / ...

随机推荐

  1. php课程---简单的分页练习

    在写代码时,我们可以用类来使代码更加方便简洁,下面是一个简单的查询分页练习 源代码: <html> <head> <style type="text/css&q ...

  2. IOS第18天(4,核心动画,时钟效果,定时器,图片旋转角度,CALayer 锚点,获取当前,小时,秒,分)

    **** #import "HMViewController.h" // 每秒秒针转6度 #define perSecendA 6 // 每分钟分针转6度 #define perM ...

  3. vmware centos6.5 net 配置

    使用NAT模式 虚拟机网络连接使用NAT模式,物理机网络连接使用Vmnet8. 虚拟机设置里面——网络适配器,网络连接选择NAT模式. 虚拟机菜单栏—编辑—虚拟网络编辑器,选择Vmnet8 NAT模式 ...

  4. websocket和swoole

    <head></head><body><script type="text/javascript">var sock = null; ...

  5. Maintaining Your Signing Identities and Certificates 维护你的签名标识和证书

    Code signing your app lets users trust that your app has been created by a source known to Apple and ...

  6. JMeter学习-004-WEB脚本入门实战

    此文为 JMeter 入门实战实例.我是 JMeter 初学菜鸟一个,因而此文适合 JMeter 初学者参阅.同时,因本人知识有限,若文中存在不足的地方,敬请大神不吝指正,非常感谢! 闲话少述,话归正 ...

  7. Windows Server 2008R2服务器安装及设置教程

    第一篇:系统安装与设置 前言本安装及设置教程适用于使用Windows2008R2为操作系统的服务器,目的是让服务器实现下列环境.语言脚本环境:ASP.ASP.Net1.1.ASP.Net2.0.ASP ...

  8. Python开发【第五章】:Python常用模块

    一.模块介绍: 1.模块定义 用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质上就是.py结尾python文件 分类:内置模块.开源模块.自定义模块 2.导入模块 本质:导 ...

  9. node + nginx + mongo搭建负载均衡

    基于node和nignx和mongo搭建负载均衡 nginx配置: upstream back {                                                  # ...

  10. Linux内核中大小端判定宏

    #include <stdio.h> ];unsigned long mylong;} endian_test = { {'l','?','?','b'} }; #define ENDIA ...