Click here to Skip to main content
12,504,695 members (62,415 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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] code block added [/edit]
Posted 8-Nov-12 22:06pm
Updated 8-Nov-12 22:20pm
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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++;
               }
           }
       }
  Permalink  
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.,.,
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

foreach(object itemChecked in checkedListBox1.CheckedItems)
{
     DataRowView castedItem = itemChecked as DataRowView;
     string comapnyName = castedItem["CompanyName"];
     int? id = castedItem["ID"];
}
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
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)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

foreach (ListItem li1 in ch11.Items)
              {
                  if (li1.Selected == true)
                  {
                      total_items[items] = li1.Text;
                      items++;
                  }
              }
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

In your code you have not assigned item with any value. Hence it is taking as garbage value.
Update the code as below.
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.

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
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160919.1 | Last Updated 21 Oct 2015
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100