Click here to Skip to main content
Rate this: bad
good
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 23:06pm
Edited 8-Nov-12 23: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 at 9-Nov-12 4:46am
   
object reference not to set an instance of the object.
i am getting this error
Umapathi K at 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 at 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 at 9-Nov-12 4:45am
   
fixed
Umapathi K at 9-Nov-12 4:47am
   
fixed?
bassike at 9-Nov-12 4:55am
   
(ch11.Items[i].Selected)
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 at 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
0 OriginalGriff 304
1 Sergey Alexandrovich Kryukov 255
2 Shweta N Mishra 216
3 Maciej Los 210
4 PIEBALDconsult 184
0 OriginalGriff 7,660
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,665


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 9 Nov 2012
Copyright © CodeProject, 1999-2014
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