CheckListBox的使用方法

CheckListBox的使用方法checklistbox控件 1.添加项 checkedListBox1.Items.Add(“蓝色”); checkedListBox1.Items.Add(“红色”); checkedListBox1.Items.Add(“黄色”);   2.判断第i项是否选中,选中为true,否则为false if(checkedListBox1.G

大家好,又见面了,我是你们的朋友全栈君。

checklistbox控件

 

1.添加项

 

checkedListBox1.Items.Add(蓝色);

 

checkedListBox1.Items.Add(红色);

 

checkedListBox1.Items.Add(黄色);

 

 

 

2. 判断第i项是否选中,选中为true,否则为false

 

ifcheckedListBox1.GetItemChecked(i)

 

{

 

     return true;

 

}

 

else

 

{

 

     return false;

 

}

 

 

 

3. 设置第i项是否选中

 

checkedListBox1.SetItemChecked(i, true); //true改为false为没有选中。

 

4. 设置全选

 

添加一个名为select_allcheckbox控件,由其控制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);     

 

}

 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/148926.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 内容大合集_十三大内容

    内容大合集_十三大内容文顶顶大神http://www.cnblogs.com/wendingding/p/3805088.html碎片知识大合集http://www.cnblogs.com/wujy/p/457161

    2022年8月5日
    7
  • linux设备驱动程序注冊过程具体解释

    linux设备驱动程序注冊过程具体解释

    2021年12月8日
    45
  • 大数据竞赛解决方案

    大数据竞赛解决方案第一章建设背景1.1政策分析2017年1月工业和信息化部正式发布了《大数据产业发展规划(2016-2020年)》,明确了“十三五”时期大数据产业的发展思路、原则和目标,将引导大数据产业持续健康发展,有力支撑制造强国和网络强国建设。2018年9月工信部公示“2018年大数据产业发展试点示范项目名单”,公布了包括大数据存储管理、大数据分析挖掘、大数据安全保障、产业创新大数据应用、…

    2022年6月1日
    209
  • matlab心形曲线代码_matlab心形

    matlab心形曲线代码_matlab心形(1)有网格线clearx=-2:0.01:2;y=sqrt(2*sqrt(x.^2)-x.^2);z=asin(abs(x)-1)-pi./2;plot(x,y);gridon;holdon;plot(x,z);axisequal;效果图(2)无网格线t=0:0.1:2*pi;x=16*sin(t).^3;y=13*cos(t)-5*cos(2*t)-2*co…

    2022年10月17日
    3
  • Redis学习——redis.conf 配置文件介绍

    学以致用 学在用前参看文章: redis.conf配置详细解析 redis.conf 配置详解 Redis配置文件详解(redis.conf)-云栖社区在Redis的使用过程,除了知道对Redis五种数据类型的操作方法之外,最主要额就是对redis.conf进行配置了,下面整理出redis.conf中常见的一些配置介绍。 参数说明 redis.conf 配置项说…

    2022年2月26日
    48
  • 稳压管稳压电路

    稳压管稳压电路一、稳压管稳压电路:整流滤波电路的输出电压会随着电网电压的波动而波动,随着负载电阻的变化而变化。为了获得稳定性好的直流电压,必须采取稳压措施。二、稳压原理:对于任何稳压电路而言,都应该从两方面来考虑:电网电压波动、负载变化,研究其输出电压是否稳定。(1)、电网电压波动:(2)、负载变化:…

    2022年6月20日
    27

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号