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. JavaAnnotation和反射简化开发

    Part I 我并不是在卖弄自己的英语有多少的了不起,只不过对Annotation这一次的解释真的很懊恼,“注解”和“注释”这两个对Annotation的翻译我听着不爽,所以全文都用Annotatio ...

  2. Silverlight控件——如何提升应用程序信任度与问题解决

    从silverlight5开始,可以在项目设置中勾选“在浏览器内运行时需要提升的信任”来达到在浏览器内运行提权silverlight客户端的目的,在个特性很有用处. 可我使用这个功能时遇到了一个奇怪的 ...

  3. redis与memcache的区别2

    总结一: memcache官方定义 Free & open source, high-performance, distributed memory object caching system ...

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

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

  5. dialog弹层的方式

    1 增加一个层<div class="dialogLayer"></div>, 要不就利用伪元素 ::before 2 利用box-shadow: 0 0 ...

  6. php如何遍历多维的stdClass Object 对象,php的转换成数组的函数只能转换外面一丛数组

    php如何遍历多维的stdClass Object 对象,php的转换成数组的函数只能转换外面一丛数组 (2012-09-10 19:58:49) 标签: 杂谈 分类: 网页基础知识 php如何遍历多 ...

  7. Android动画设计源码地址

    Android动画设计源码地址 http://blog.csdn.net/shanghaibao123/article/details/45223825

  8. Win8.1密钥

    Win8.1 在线永久激活密钥一枚!  78BHN-M3KRH-PCP9W-HQJYR-Q9KHD [剩余次数:7K多+] 继续增加 [Key]:HPCJW-VGYW4-CR7W2-JG6Q7-K4Q ...

  9. nginx优化 突破十万并发

    一.一般来说nginx 配置文件中对优化比较有作用的为以下几项: 1.  worker_processes 8; nginx 进程数,建议按照cpu 数目来指定,一般为它的倍数 (如,2个四核的cpu ...

  10. linux连接与传输等命令

    linux 远程连接windows rdesktop -a 16 223.223.111.8 远程连接到windows服务器 rdesktop -a 16 -r disk:home=/home 223 ...