private void Form1_Load(object sender, EventArgs e)
        {

            checkedListBox1.Items.Add("语文");
            checkedListBox1.Items.Add("数学");
            checkedListBox1.Items.Add("外语");
            checkedListBox1.Items.Add("政治");
            checkedListBox1.Items.Add("历史");
            checkedListBox1.Items.Add("地理");
            checkedListBox1.Items.Add("体育");

        }

        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strCollected = string.Empty;
            ; 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]);
                    }
                    label1.Text=strCollected;
                }
            }

//for (int i = 0; i < checkedListBox1.Items.Count; i++)
//{
   // if (checkedListBox1.GetItemChecked(i))
   // {
      //  MessageBox.Show(checkedListBox1.GetItemText(checkedListBox1.Items[i]));
   // }
//}

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

一般认为:foreach (object obj in checkedListBox1.SelectedItems)即可遍历选中的值。
其实这里遍历的只是高亮的值并不是打勾的值。遍历打勾的值要用下面的代码:

复制代码
; i < checkedListBox1.Items.Count; i++)
{
    if (checkedListBox1.GetItemChecked(i))
    {
        MessageBox.Show(checkedListBox1.GetItemText(checkedListBox1.Items[i]));
    }
}
最近用到checklistbox控件,在使用其过程中,花了较多的时间,这里我收集了其相关的代码段,希望对大家有所帮助。
.
添加项
checkedListBox1.Items.Add("蓝色");
checkedListBox1.Items.Add("红色");
checkedListBox1.Items.Add("黄色");

.
判断第i项是否选中,选中为true,否则为false
if(checkedListBox1.GetItemChecked(i))
{
     return true;
}
else
{
     return false;
}

.
设置第i项是否选中
checkedListBox1.SetItemChecked(i, true);  //true改为false为没有选中。

.
设置全选
添加一个名为select_all的checkbox控件,由其控制checkedListBox是全选还是全不选。
private void select_all_CheckedChanged(object sender, EventArgs e)
{
     if(select_all.Checked)
{
          ; j < checkedListBox1.Items.Count; j++)
               checkedListBox1.SetItemChecked(j, true);
}
else
{
; j < checkedListBox1.Items.Count; j++)
      checkedListBox1.SetItemChecked(j, false);
}
}

.
得到全部选中的值 ,并将选中的项的文本组合成为一个字符串。
 string strCollected = string.Empty;
 ; 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]);
           }
       }
}

.
设置CheckedListBox中第i项的Checked状态
checkedListBox1.SetItemCheckState(i, CheckState.Checked);

.
private void checkBoxAll_CheckedChanged(object sender, EventArgs e)
{
     if (checkBoxAll.Checked)
     {
         //被选择了则将CheckedListBox中的所有条目都变为Checked状态
         ; i < checkedListBoxLayerControl.Items.Count;
                   i++)
         {
checkedListBoxLayerControl.SetItemCheckState(i,
        CheckState.Checked);
}
}
else
{
     //否则变成Unchecked状态
    ;
 i < checkedListBoxLayerControl.Items.Count; i++)
{
checkedListBoxLayerControl.SetItemCheckState(i, CheckState.Unchecked);
}
}
}

.
checkedListBox 单选设置(代码实现)
private void chkl_ItemAuditing_ItemCheck(object sender,
ItemCheckEventArgs e)
{
     )
    {
         ; i < chkl_ItemAuditing.Items.Count; i++)
         {
if (i != e.Index)
{
this.chkl_ItemAuditing.SetItemCheckState(i,
System.Windows.Forms.CheckState.Unchecked);
}
}
}
}

.
checkedListBox1显示一个数据库中关键字对应的所有记录
; i < table.Rows.Count; i++)
{
    string name = table.Rows["myname"].ToString();
    string paw = table.Rows["mypaw"].ToString();
    checkedListBox1.Items.Add(name + paw);
}

.
;i<CheckedListBox.Items.Count;i++)
{
   if(CheckedListBox.GetItemText(
CheckedListBox.Items)=="你得到的值")
{
      CheckedListBox.SetItemChecked(i,true);
}
}

.
清除checkedListBox1中所有的选项
; i < checkedListBox1.Items.Count; i++)
{
    checkedListBox1.Items.Clear();
}

.
//设置索引为index的项为选中状态
; i < checkedListBox1.Items.Count; i++)
{
    checkedListBox1.SetItemChecked(i, true);
} 

.
; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetSelected(i))
{
MessageBox.Show(checkedListBox1.CheckedItems.ToString());
}
}

.
//选中checkedListBox1所有的选项

; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.SetItemCheckState(i, CheckState.Checked);
}

.
; i < checkedListBox1.Items.Count; i++)
{
//如果checkedListBox1的第i项被选中,
//则显示checkedListBox1对应的值
if (checkedListBox1.GetItemChecked(i))
{
     MessageBox.Show(checkedListBox1.Items.ToString());
}
}

.
//反向选择checkedListBox1的选项
; i < checkedListBox1.Items.Count; i++)
{
    if (checkedListBox1.GetItemChecked(i))
   {
       checkedListBox1.SetItemChecked(i, false);
   }
   else
   {
       checkedListBox1.SetItemChecked(i, true);
   }
}

.
//checkedListBox1中选定的项->checkedListBox2
; i < checkedListBox1.CheckedItems.Count; i++)
{
     checkedListBox2.Items.Add(this.checkedListBox1.CheckedItems);

//remove是除去一个具体的值,不是index,注意了
     this.checkedListBox1.Items.Remove(
         this.checkedListBox1.CheckedItems);
}

C# 复选框显示多项选择的更多相关文章

  1. HTML--使用单选框、复选框,让用户选择

    在使用表单设计调查表时,为了减少用户的操作,使用选择框是一个好主意,html中有两种选择框,即单选框和复选框,两者的区别是单选框中的选项用户只能选择一项,而复选框中用户可以任意选择多项,甚至全选.请看 ...

  2. php中表单提交复选框与下拉列表项

    在赶项目中,抽出半个小时来写篇博客吧,这个功能说实话不难,为什么要写呢,因为在复选框那里有小小的难点,我试了好多遍才试成功的,希望能为以后需要帮助的同学提供点思路. 先看一下我做的效果吧 就是给每个业 ...

  3. Selenium3自动化测试【28】单选框、复选框、下拉选择框

    Html页面中的单选按钮.复选框.下拉框均可通过WebDriver实现操做.本节结合案例一起来看看WebDriver如何操做这些控件. 同步视频知识与系列知识内容,可关注:[公众号]:柒哥测试:[WX ...

  4. [SAP ABAP开发技术总结]选择屏幕——按钮、单选复选框

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  5. checkebox 全选 ,子复选框单个全部选择后,全选框也会被选择

    <script> //点击全选,子复选框被选中 function demo(){ var allcheck=document.getElementById("allcheck&q ...

  6. WPF: 实现带全选复选框的列表控件

    本文将说明如何创建一个带全选复选框的列表控件.其效果如下图:     这个控件是由一个复选框(CheckBox)与一个 ListView 组合而成.它的操作逻辑: 当选中“全选”时,列表中所有的项目都 ...

  7. WPF实现带全选复选框的列表控件

    本文将说明如何创建一个带全选复选框的列表控件.其效果如下图: 这个控件是由一个复选框(CheckBox)与一个 ListView 组合而成.它的操作逻辑: 当选中“全选”时,列表中所有的项目都会被选中 ...

  8. 单选按钮(RadioButton)与复选框(CheckBox)的功能与用法

    单选按钮(RadioButton)和复选框(CheckBox).状态开关按钮(ToggleButton)与开关(Switch)是用户界面中最普通的UI组件,他们都继承了Button类,因此都可直接使用 ...

  9. 安卓开发:UI组件-RadioButton和复选框CheckBox

    2.5RadioButton 为用户提供由两个及以上互斥的选项组成的选项集. 2.5.1精简代码 在按钮变多之后,多次重复书写点击事件有些繁琐,我们在这里创建一个事件OnClick,每次点击时调用该事 ...

随机推荐

  1. vue之简单组件例子

    <!-- 根组件 --> <!-- vue的模板内,所有内容要被一个根节点包含起来 --> <template> <div id="app" ...

  2. tp5入门

    runtime目录里的文件是临时文件,可随时删除 在tp5里,命名空间对应了文件的所在目录,app命名空间通常代表了文件的起始目录为application,而think命名空间则代表了文件的起始目录为 ...

  3. 递归遍历所有xml的节点及子节点

    import java.io.File; import java.util.List; import org.dom4j.Attribute; import org.dom4j.Document; i ...

  4. Python-web应用 +HTTP协议 +web框架

    web架构 # web应用 架构# C/S 架构 | B/S 架构# client server: 客户端服务器架构,C++# browser server:浏览器服务器架构,Java.Python ...

  5. 十.nginx反向代理负载均衡服务实践部署

    期中集群架构-第十章-nginx反向代理负载均衡章节章节====================================================================== 0 ...

  6. fs.inotify.max_user_watches默认值太小,导致too many open files

    运行环境:centos7.5 linux 打开文件数 too many open files 解决方法fs.inotify.max_user_watches默认值太小,导致too many open ...

  7. 服务器与本地的控制工具unison

    中文文档:https://wiki.archlinux.org/index.php/Unison_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87) 下载:http://un ...

  8. 金蝶KIS&K3助记码SQL数据库批量刷新

    金蝶KIS&K3助记码SQL数据库批量刷新 用的次数不多,就没有写入存储过程或者触发里面了,可以自行实现. 第一步选择对应账套的数据库,执行下面的命令,这个是一个函数. go if exist ...

  9. GRPC单向/双向流

    开始食用grpc(之二)https://www.cnblogs.com/funnyzpc/p/9570992.html 开始食用grpc(之一)https://www.cnblogs.com/funn ...

  10. Mybatis Annotation使用小结

    Mybatis Annotation使用小结 之前一直有看过mybatis的注解使用方式,但没有去看过它的原理.今天看springboot-mybatis-annotation使用的时候,debug了 ...