Click here to Skip to main content
16,021,172 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have almost searched everywhere on net as well as tried every possible code i know but still no success..I would appreciate if some one may guide me?Following is my code.

C#
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int sum = 0;
            foreach (object itemChecked in checkedListBox1.CheckedItems)
            {
                sum += Int32.Parse(checkedListBox1.SelectedItem.ToString());
            }
}
Posted
Comments
agent_kruger 9-Mar-14 5:16am    
sir, i don't understand . You have the code now what is the problem? Is it showing error or something else?

As Marcin had mentioned, instead using SelectedItem event, use ItemCheck[^], but i'd warn you that the check state is not updated until after the ItemCheck event occurs.
CheckedListBox Events[^]
 
Share this answer
 
Comments
BillWoodruff 8-Mar-14 20:59pm    
Fortunately an ItemCheck Event will (almost always ... yes the Control is not perfect) be followed by a ValueChanged Event, so you can use that to correctly evaluate the state of all Items Checked. See below for an example.
Since an 'ItemCheck Event is always (well, it should be: see note below) followed by a ValueChanged Event (yes, that doesn't make a lot of sense, but that's the way it is), you can do this to make sure your 'sum calculation is always current.

This example assumes you have a multi-line TextBox on a Form, 'textBox1, and a CheckedListBox, 'checkedListBox1:
C#
// flag to determine a change in CheckState of any Item
private bool IsCheckedChanged = false;

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
    IsCheckedChanged = true;

    // for debugging only
    textBox1.Text += "change in Item CheckState" + Environment.NewLine;
}

private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    // for debugging only
    textBox1.Text += "index changed" + Environment.NewLine + Environment.NewLine;
}

private void checkedListBox1_SelectedValueChanged(object sender, EventArgs e)
{
    // for debugging only
    textBox1.Text += "value changed" + Environment.NewLine;

    if (IsCheckedChanged)
    {
        int sum = 0;

        foreach (var chkItem in checkedListBox1.CheckedItems)
        {
            sum += Convert.ToInt32(chkItem);
        }

        // for debugging only
        textBox1.Text += "Sum: " + sum.ToString() + Environment.NewLine;

        IsCheckedChanged = false;
    }
}
If click Items rapidly in the CheckedListBox, and examine the content written to the TextBox, you will probably observe some instances where there is an irregularity in the way the various Events are fired. Yep, the Control ain't perfect !

But, most of the time a change in any Item's CheckState will trigger a ValueChanged Event, and you can evaluate the state of all CheckedItems reliably.

In using the native Windows Controls (or third-party Controls !), I strongly suggest you experiment with writing simple EventHandlers for the Events you are interested in that write some meaningful output to the Console or to a TextBox. Learning, and understanding, the order of execution, and inter-dependencies of the Events, is, imho, the only way to really be sure you understand how to use the given Control efficiently, and without creating subtle errors that are hard to understand/debug.
 
Share this answer
 
Comments
Maciej Los 9-Mar-14 7:41am    
Very good advice with sample code!
+5
Why are you using SelectedItem property while you iterating CheckedItems?? This will work:
C#
int sum = 0;
foreach (object item in checkedListBox1.CheckedItems)
{
    sum += Int32.Parse(item.ToString());
}
 
Share this answer
 
Comments
Maciej Los 8-Mar-14 14:49pm    
You're right, Marcin, but OP don't know what kind of event He/She should use. Please, see my answer.
Cheers!
Maciej
Marcin Kozub 8-Mar-14 15:03pm    
Yep, didn't read too carefully ;)
Thank You every one for your valuable time and solution but i have tried all the solution yet i get an exception saying "Input was not in a correct format when i tick the check box.
 
Share this answer
 

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