Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
3.86/5 (3 votes)
See more:
i have a checkboxlist with 3 items...but i want to check only one item at a time...how to do this in c#?
Posted
Comments
Tom Marvolo Riddle 21-Jan-14 6:44am    
Try it in RadioButtonList.
Member 10276220 21-Jan-14 6:44am    
no buddy...i need it in checkboxlist

Use SelectedIndexChanged event of checkedListBox1 .
Refer following code.

C#
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
       {
           int iSelectedIndex = checkedListBox1.SelectedIndex;
           if (iSelectedIndex == -1)
               return;
           for (int iIndex = 0; iIndex < checkedListBox1.Items.Count; iIndex++)
               checkedListBox1.SetItemCheckState(iIndex, CheckState.Unchecked);
           checkedListBox1.SetItemCheckState(iSelectedIndex, CheckState.Checked);
       }
 
Share this answer
 
use for loop like this


for(int i=0;i<chklist.items.count;i++)>
{
    if(chkList.GetItemChecked(i) == true)
    {
        //true
    }
}
 
Share this answer
 
Try this:
C#
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    int selNdx = checkedListBox1.SelectedIndex;

    foreach(int cbNdx in checkedListBox1.CheckedIndices)
    {
        if(cbNdx != selNdx)
        {
            checkedListBox1.SetItemChecked(cbNdx, false);
        }
    }
}
By using the 'CheckedIndices property of the CheckedListBox, you know which items are checked, and can test only those. Using this code, you don't have to deal with the fact that each Item in the CheckedListBox is Type 'object.
 
Share this answer
 
 
Share this answer
 
Comments
Member 10276220 21-Jan-14 7:08am    
i want it in c#
Karthik_Mahalingam 21-Jan-14 7:20am    
ok.. kindly check after some time...

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