如何从checkedlistbox中获取所选项目 [英] how to get selected item from checkedlistbox

查看:65
本文介绍了如何从checkedlistbox中获取所选项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它的Windows应用程序。我想从复选框列表中选择项目(文本)

并将其存储到数组中。



i在编码之下使用但不是功能



这里ch11是一个checkedlistbox名称。





its windows application. i want to get selected item(text) from checkedbox list
and store it into array.

i used below coding but its not funcationing

here ch11 is a checkedlistbox name.


private void button1_Click(object sender, EventArgs e)
      {
          string[] total_items = new string[50];
          int items;


          try
          {


              foreach (ListItem li1 in ch11.CheckedItems)
              {
                  if (li1.Selected == true)
                  {
                      total_items[items] = li1.Text;
                      items++;
                  }
              }
          }
      }





[]编辑]添加了代码块[/ edit]

推荐答案

ch11.CheckedItems只给你检查项目,所以你不需要再次检查:



ch11.CheckedItems will give u only checked items, so u dont need to check again:

private void button1_Click(object sender, EventArgs e)
       {
           string[] total_items = new string[50];
           int items;


           try
           {
               foreach (ListItem li1 in ch11.CheckedItems)
               {
                    total_items[items] = li1.Text;
                    items++;
               }
           }
       }


foreach(object itemChecked in checkedListBox1.CheckedItems)
{
     DataRowView castedItem = itemChecked as DataRowView;
     string comapnyName = castedItem["CompanyName"];
     int? id = castedItem["ID"];
}


items = 0;
for(int i = 0; i < CheckBoxList1.Items.Count; i++)
{
     if(CheckBoxList1.Items[i].Selected)
     {
         total_items[items] = CheckBoxList1.Items[i].Value;
         items++;
     }
}



必须有效!

最好的问候


must work!
Best Regards


这篇关于如何从checkedlistbox中获取所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆