简体   繁体   中英

checkedlistbox operation in c#

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?

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());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM