Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i need to count how many checkbox got selected in the checkedlistbox
Posted

Here you go:

C#
var count = checkedListBox1.SelectedItems.Count;
 
Share this answer
 
Comments
StackQ 8-Nov-12 3:50am    
it's perfect answer
jim lahey 8-Nov-12 3:54am    
thanks. glad to be of help.
StackQ 8-Nov-12 3:57am    
it's okay
Umapathi K 8-Nov-12 4:00am    
i am checking validation like if user select more than 2 items in a checkedlistbox should throw error and last selected checkbox should get uncheck. i am getting error message but last checkbox getting selected.
jim lahey 8-Nov-12 4:08am    
If you want help with this post it as a question. It will just get lost in all the comments otherwise.
C#
int icount = 0;
foreach (ListItem lItem in Listbox.Items)
{
 if (lItem.Selected == true)
 {
   icount++
 }
}
 
Share this answer
 
Comments
Umapathi K 8-Nov-12 3:59am    
i am checking validation like if user select more than 2 items in a checkedlistbox should throw error and last selected checkbox should get uncheck. i am getting error message but last checkbox getting selected.
you can do like

XML
for (int i=0; i<checkboxlist1.Items.Count; i++)
         {

            if (checkboxlist1.Items[i].Selected)
            {

               Message.Text += checkboxlist1.Items[i].Text + "<br>";

            }

         }


For more detail follow msdn link
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkboxlist%28v=vs.71%29.aspx[^]
 
Share this answer
 
Comments
Umapathi K 8-Nov-12 4:00am    
i am checking validation like if user select more than 2 items in a checkedlistbox should throw error and last selected checkbox should get uncheck. i am getting error message but last checkbox getting selected.
run loop for every check box,check true or false if true then checkboxCount++ and if false then do nothing ,At the end of loop at variable checkboxCount u will find how many checkbox checked.
 
Share this answer
 
Comments
Umapathi K 8-Nov-12 4:00am    
i am checking validation like if user select more than 2 items in a checkedlistbox should throw error and last selected checkbox should get uncheck. i am getting error message but last checkbox getting selected.
StackQ 8-Nov-12 4:03am    
with my logic after the value of checkboxCount++, u can check it in if-else condition and if,it's value is greater than >2(what u want u can put 2,3,4...) then show error else nothing.
Umapathi K 8-Nov-12 4:08am    
when error message appears after that if user try to choose more than 2 checkbox getting select., it shud not happen
StackQ 8-Nov-12 4:13am    
so u have to give a textbox where user will input how many check box he wants to check,then set this value at greater than > (as user specify)

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