Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.


C#
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] code block added [/edit]
Posted
Updated 8-Nov-12 22:20pm
v3

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

C#
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++;
               }
           }
       }
 
Share this answer
 
Comments
Umapathi K 9-Nov-12 4:46am    
object reference not to set an instance of the object.
i am getting this error
Umapathi K 9-Nov-12 4:52am    
working now.,.,
C#
foreach(object itemChecked in checkedListBox1.CheckedItems)
{
     DataRowView castedItem = itemChecked as DataRowView;
     string comapnyName = castedItem["CompanyName"];
     int? id = castedItem["ID"];
}
 
Share this answer
 
C#
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
 
Share this answer
 
v3
Comments
Umapathi K 9-Nov-12 4:38am    
for (int i = 0; i < ch11.Items.Count; i++)
{
if (ch11.Items[i].Selected == true) //error in this line
{
total_items[items] = ch11.Items[i].Value;
items++;
}
}
bassike 9-Nov-12 4:45am    
fixed
Umapathi K 9-Nov-12 4:47am    
fixed?
bassike 9-Nov-12 4:55am    
(ch11.Items[i].Selected)
C#
foreach (ListItem li1 in ch11.Items)
              {
                  if (li1.Selected == true)
                  {
                      total_items[items] = li1.Text;
                      items++;
                  }
              }
 
Share this answer
 
In your code you have not assigned item with any value. Hence it is taking as garbage value.
Update the code as below.
C#
private void button1_Click(object sender, EventArgs e)
        {
            string[] total_items = new string[50];
            int items = 0;


            try
            {


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


The best way is to use List of string insted of array of string.

C#
private void button1_Click(object sender, EventArgs e)
        {
            List<string> lstString = new List<string>();
            try
            {
                foreach (ListItem li1 in ch11.CheckedItems)
                {
                    if (li1.Selected == true)
                    {
                        lstString.Add(li1.Text);
                    }
                }
            }
            catch
            {
            }
        }<pre>

Hope this will help you
 
Share this answer
 
v3
Comments
Umapathi K 9-Nov-12 4:43am    
system.collections.generic.list[system.string]
getting this error

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900