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. Windows 8.1升级至Windows 10后,启动VisualSVN Server Manager报错:提供程序无法执行所尝试的操作 (0x80041024)的解决

    1.1.Windows 8.1升级至Windows 10后,启动VisualSVN Server Manager报错:提供程序无法执行所尝试的操作 (0x80041024),VisualSVN Ser ...

  2. SVN 远程无法联通

    远程花生壳搭建之后,配置在服务器iis上的其他的网站都能访问,局域网都可以,就是SVN远程连接不通. 没有搞过这样的问题,一下就不知道怎么办了.网上也没有人搞过,然后想到以前公司的一个大神搞过,然后请 ...

  3. IOS第16天(4,Quartz2D柱状图)

    *** #import "HMBarView.h" #import "UIColor+Random.h" @implementation HMBarView - ...

  4. dom4j解析xml文档(增删改查)

    package itcast.dom4j; import java.io.File; import java.io.FileOutputStream; import java.io.FileWrite ...

  5. Linux ACL管理详解

    转自: http://linuxguest.blog.51cto.com/195664/124107 1. 为什么要使用ACL先让我们来简单地复习一下Linux的file permission.在li ...

  6. Educational Codeforces Round 16 A B C E

    做题太久也有点累了..难题不愿做 水题不愿敲..床上一躺一下午..离下一场div2还有点时间 正好有edu的不计分场 就做了一下玩玩了 D是个数学题 F是个AC自动机 都没看明白 留待以后补 A 给出 ...

  7. NEC学习 ---- 模块 -水平文字链接列表

    HTML代码: <div class="container"> <div class="m-list1"> <ul class=& ...

  8. J2EE用户CPU占用过大后的分析过程

        1.找到最耗CPU的java线程ps命令 命令:ps -mp pid -o THREAD,tid,time   或者  ps -Lfp pid 结果展示:            2.可以获取到 ...

  9. c语言中的指针问题

    “*”符号的作用在C语言中有两种: 1.声明该变量是指针,例如:int * p;//表示声明一个int类型的指针,变量名为p 2.在指针运算时,表示取这个地址上的内容,例如  temp = *p;// ...

  10. oracle 判断字符串是否日期格式

    select case when to_char(TO_DATE(NVL('2015- 8', 'a'), 'yyyy-mm'),'yyyy-mm')='2015- 8' then 1 else 0 ...