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. Android课程---用进度条改变图片透明度

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  2. js判断移动终端url跳转

    CODE <script> //判断终端url跳转 function sp_isMobile() { return Boolean(navigator.userAgent.match(/. ...

  3. centos同步北京时间

    yum install ntp ntpdate #ntpdate -u 202.120.2.101 //写入硬件 #hwclock -w 以下是国内常见的NTP服务器 ntp.sjtu.edu.cn ...

  4. Flink -- Barrier

    CheckpointBarrierHandler 这个接口用于react从input channel过来的checkpoint barrier,这里可以通过不同的实现来,决定是简单的track bar ...

  5. book

    http://www.ed2000.com/ShowFile.asp?FileID=61391 e-itbook.com

  6. JS之获取样式

    基本代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  7. C语言 ---- 基本数据类型和基本运算 iOS学习-----细碎知识点总结

    // 导入头文件(stdio.h),标准输入输出的头文件,#include <stdio.h> // 程序的入口int main(int argc, const char * argv[] ...

  8. C++中的explicit关键字

    http://www.cnblogs.com/winnersun/archive/2011/07/16/2108440.html 上面链接中的博主写的很好,我也不多说了.举得例子也很好,应该也是看了E ...

  9. Comet 反Ajax: jQuery与PHP实现Ajax长轮询

    原文地址(http://justcode.ikeepstudying.com/2016/08/comet-%E5%8F%8Dajax-%E5%9F%BA%E4%BA%8Ejquery%E4%B8%8E ...

  10. struts 数据验证

    1. validate()验证 将对页面表单验证的内容写到validate()方法中,实现验证和业务处理内容的分离 在Action中添加 validate()方法   public void vali ...