Reputation: 9074
I am developing a C# application in which a CheckedListBox is used.
The CheckedListBox contains subject names like: Accounting, OC, Physics, Economics, Maths, etc...
I have one database table in which fees are associated with particular subjects:
Eg table name Fee:
Subject | Fee
Accounting 2000
Economics 3000
OC 2000
I want to build the application such that when a user clicks on a subject in the CheckedListBox, it's fees should automatically be added to a total.
For that I have tried different CheckedListBox events: Click
, SelectedIndexChanged
, ItemCheck
, etc...
But I am not getting the correct behavior when I make the first check on the CheckedListBox.
For the testing I have added the following subjects to the CheckedListBox: Accounting, Economics and Maths.
And this is what I have in my event handler:
for(int i=0; i<chlstbox.CheckedItems.Count; i++)
{
messagebox.show(chklstbox.CheckedItems[i].ToString());
}
When I check the first subject Accounting then it does not shows me any message box. But when I click on Economics it shows me Accounts and then Economics.
In this case it should show me Accounting when i first click on Accounting. And there is no reply when I un-check the subjects.
What should I do? Is there any particular event or technique associated with it which I should use?
Upvotes: 0
Views: 429
Reputation: 54532
If you look at the ItemChecketEventArgs you will see the CurrentValue, OldValue and Index of the Item that is being Selected. So try something like this. The ItemCheck Event lets you know what is changing before it physically is changed.
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (e.NewValue == CheckState.Checked)
{
MessageBox.Show(checkedListBox1.Items[e.Index].ToString());
}
}
Upvotes: 1