码迷,mamicode.com
首页 > 其他好文 > 详细

C# CheckedListBox控件的使用方法

时间:2014-07-22 23:20:28      阅读:383      评论:0      收藏:0      [点我收藏+]

标签:style   使用   os   strong   数据   art   

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
显示一个数据库中keyword相应的全部记录


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);      
}

18.

CheckedlistBox控件比較实用到两个属性分别为CheckOnClick为True:表示单击就选中当前行,为False:要点两下才干够选中。(默认值为False)。另一个属性为ThreeDCheckBoxes为True:表示三维的选中标记,为False:表示表面的显示标记。(默认值为False)。

19.

for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                if (checkedListBox1.GetItemChecked(i))
                {
                    checkedListBox1.SelectedIndex = i;//
利用SelectedValue取得Value值时,仅仅能取得当前焦点项的值。所以要对整个CheckedListBox中的全部勾选项,让其都做一次焦点项才干取得全部勾选的项的值。
                    str+=  checkedListBox1.SelectedValue;
                }
            }

20.绑定数据

checkedListBox1.DataSource = dt;
checkedListBox1.DisplayMember = "item";
checkedListBox1.ValueMember = "code";

 

近期用到checklistbox控件,在使用其过程中,花了较多的时间,这里我收集了其相关的代码段,希望对大家有所帮助。

C# CheckedListBox控件的使用方法,布布扣,bubuko.com

C# CheckedListBox控件的使用方法

标签:style   使用   os   strong   数据   art   

原文地址:http://www.cnblogs.com/mfrbuaa/p/3861419.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!