---
title: "全面介绍 C#Checklistbox用法(转)"
author: "cfeers"
date: "2012-05-11T09:08:50Z"
tags: []
categories: ["Web前端"]
slug: "c-checklistbox"
draft: false
lastmod: "2012-05-11T09:08:50Z"
id: 1522596
url: "https://cfeers.iteye.com/blog/1522596"
---

本文从6各方面对c#checklistbox用法做详细介绍，他们分别是c#checklistbox的用法的添加项、判断第0项是否选中、设置第0项是否选中、设置全选、得到全部选中的值、数据绑定。

**1、c#checklistbox用法之添加项：**

```


checkedListBox1.Items.Add(
"蓝色"
);  




checkedListBox1.Items.Add(
"红色"
);  



checkedListBox1.Items.Add(
"黄色"
); 


```

**2、c#checklistbox用法之判断第0项是否选中**

```

if
 (checkedListBox1.GetItemChecked(0)) 


```

**3、c#checklistbox用法之设置第0项是否选中**

```

checkedListBox1.SetItemChecked(0, 
true
); 


```

**4、c#checklistbox用法之设置全选**

添加一名为select\_all的checkbox控件

```


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、c#checklistbox用法之得到全部选中的值：**

```


private
 
void
 linkLabel_yes_LinkClicked(
object
 sender, LinkLabelLinkClickedEventArgs e)  




       {  



           panel_friend.Visible = 
false
;  



           button_friend.Text = 
"好友面板"
;  



           sms_str = 
null
;  



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



               
if
(checkedListBox1.GetItemChecked(j))  



               {  



                   
//do 
 



               }    



       }  


```

**6、c#checklistbox用法之数据绑定**

```


protected
 
void
 Page_Load(
object
 sender, EventArgs e)  




    {  



        
if
(!Page.IsPostBack)    
//这句很重要,如果不加,则每次加载时都要执行一次绑定,易丢失数据. 
 



        {  



        
//创建链接; 
 



        SqlConnection con = DB.createConnection();      
//不再用 new  
 



        con.Open();  



        SqlCommand cmd = 
new
 SqlCommand(
"select * from [personlike]"
,con);  



        SqlDataReader sdr = cmd.ExecuteReader();  



        
this
.CheckBoxList1.DataTextField = 
"likeContent"
;     



        
this
.CheckBoxList1.DataValueField = 
"ID"
;  



        
this
.CheckBoxList1.DataSource = sdr;  



        
this
.CheckBoxList1.DataBind();  



        sdr.Close();        
//关闭记录集 
 



        con.Close();        
//关闭链接 
 



        }  



    }  



//单击按钮读取所做的操作(数据),用Response.Write()方法显示. 
 



    
protected
 
void
 Button1_Click(
object
 sender, EventArgs e)  



    {  



        
for
(
int
 i=0;i<=
this
.CheckBoxList1.Items.Count-1;i++)  



        {  



            
if
(
this
.CheckBoxList1.Items[i].Selected)  



            {  



                Response.Write(
this
.CheckBoxList1.Items[i].Value.ToString() +
"-"
+
this
.CheckBoxList1.Items[i].Text + 
"

"
);  



            }  



        }  



    } 

























checkedListBox控件



1. 功能



CheckedListBox控件扩展了ListBox控件，它几乎能完成列表框可以完成的所有任务，并且还可以在列表中的项旁边显示复选标记。

CheckedListBox控件与ListBox控件的主要差异在于复选列表框只能有一项选中或未选中任何项。注意，选定的项在窗体上突出显示，与已选

中的项不同。图1为CheckedListBox控件。








图1 CheckedListBox控件



2．属性



CheckedListBox 控件常用属性及说明如表1所示。








表1       CheckedListBox控件常用属性及说明



下面对比较重要的属性进行详细介绍。



（1）Items属性。此属性用于向控件中添加项。



语法：



public ObjectCollection Items { get; }



属性值：代表CheckedListBox中项的CheckedListBox.ObjectCollection集合。



（2）CheckedItems属性。此属性是指CheckedListBox控件中所有选中项的集合。



语法：



public CheckedItemCollection CheckedItems { get; }



属性值：CheckedListBox的CheckedListBox.CheckedItemCollection集合。



示例



Items属性的使用



本示例实现效果为：当程序运行时，单击【确定】按钮，利用Items属性将选择的项添加到Checked ListBox 控件中。



程序主要代码如下：



if (this.checkBox1.Checked == True)



{



this.checkedListBox1.Items.Add(checkBox1.Text);



}



if (this.checkBox2.Checked == True)



{



this.checkedListBox1.Items.Add(checkBox2.Text);



}



if (this.checkBox3.Checked == True)



{



this.checkedListBox1.Items.Add(checkBox3.Text);



}



3．方法



CheckedListBox控件常用方法及说明如表2所示。








表2       CheckedListBox控件常用方法及说明



下面详细介绍SetItemCheckState方法，此方法用于设置当前的复选状态。



语法：



public void SetItemCheckState (int index,CheckState value)



index：要为其设置状态的项的索引。



value：CheckState值之一。
```

转自[http://blog.csdn.net/kable999/article/details/5006275](http://blog.csdn.net/kable999/article/details/5006275)