`
cfeers
  • 浏览: 134816 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

全面介绍 C#Checklistbox用法(转)

 
阅读更多

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

1、c#checklistbox用法之添加项:

  1. checkedListBox1.Items.Add( "蓝色" );  
  2. checkedListBox1.Items.Add( "红色" );  
  3. checkedListBox1.Items.Add( "黄色" ); 

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

  1. if  (checkedListBox1.GetItemChecked(0)) 

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

  1. checkedListBox1.SetItemChecked(0,  true ); 

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

添加一名为select_all的checkbox控件

  1. private   void  select_all_CheckedChanged( object  sender, EventArgs e)  
  2.         {  
  3.              if (select_all.Checked)  
  4.                  for  ( int  j = 0; j < checkedListBox1.Items.Count; j++)  
  5.                     checkedListBox1.SetItemChecked(j,  true );  
  6.              else  
  7.                  for  ( int  j =0; j < checkedListBox1.Items.Count; j++)  
  8.                     checkedListBox1.SetItemChecked(j,  false );  
  9.         } 

5、c#checklistbox用法之得到全部选中的值:

  1. private   void  linkLabel_yes_LinkClicked( object  sender, LinkLabelLinkClickedEventArgs e)  
  2.        {  
  3.            panel_friend.Visible =  false ;  
  4.            button_friend.Text =  "好友面板" ;  
  5.            sms_str =  null ;  
  6.             for  ( int  j = 0; j < checkedListBox1.Items.Count; j++)  
  7.                 if (checkedListBox1.GetItemChecked(j))  
  8.                {  
  9.                     //do  
  10.                }    
  11.        }  

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

  1. protected   void  Page_Load( object  sender, EventArgs e)  
  2.     {  
  3.          if (!Page.IsPostBack)     //这句很重要,如果不加,则每次加载时都要执行一次绑定,易丢失数据.  
  4.         {  
  5.          //创建链接;  
  6.         SqlConnection con = DB.createConnection();       //不再用 new   
  7.         con.Open();  
  8.         SqlCommand cmd =  new  SqlCommand( "select * from [personlike]" ,con);  
  9.         SqlDataReader sdr = cmd.ExecuteReader();  
  10.          this .CheckBoxList1.DataTextField =  "likeContent" ;     
  11.          this .CheckBoxList1.DataValueField =  "ID" ;  
  12.          this .CheckBoxList1.DataSource = sdr;  
  13.          this .CheckBoxList1.DataBind();  
  14.         sdr.Close();         //关闭记录集  
  15.         con.Close();         //关闭链接  
  16.         }  
  17.     }  
  18. //单击按钮读取所做的操作(数据),用Response.Write()方法显示.  
  19.      protected   void  Button1_Click( object  sender, EventArgs e)  
  20.     {  
  21.          for ( int  i=0;i<= this .CheckBoxList1.Items.Count-1;i++)  
  22.         {  
  23.              if ( this .CheckBoxList1.Items[i].Selected)  
  24.             {  
  25.                 Response.Write( this .CheckBoxList1.Items[i].Value.ToString() + "-" + this .CheckBoxList1.Items[i].Text +  " " );  
  26.             }  
  27.         }  
  28.     }
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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics