简体   繁体   English

C#中的checkedlistbox操作

[英]checkedlistbox operation in c#

I am developing a C# application in which a CheckedListBox is used. 我正在开发其中使用CheckedListBox的C#应用​​程序。

The CheckedListBox contains subject names like: Accounting, OC, Physics, Economics, Maths, etc... CheckedListBox包含以下科目名称:会计,OC,物理,经济学,数学等。

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. 我想构建该应用程序,以便当用户单击CheckedListBox中的主题时,其费用应自动添加到总计中。

For that I have tried different CheckedListBox events: Click , SelectedIndexChanged , ItemCheck , etc... 为此,我尝试了不同的CheckedListBox事件: ClickSelectedIndexChangedItemCheck等。

But I am not getting the correct behavior when I make the first check on the CheckedListBox. 但是,当我在CheckedListBox上进行第一次检查时,我没有得到正确的行为。

For the testing I have added the following subjects to the CheckedListBox: Accounting, Economics and Maths. 为了进行测试,我在CheckedListBox中添加了以下主题:会计,经济学和数学。

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. 如果查看ItemChecketEventArgs ,将看到正在选择的项目的CurrentValue,OldValue和Index。 So try something like this. 所以尝试这样的事情。 The ItemCheck Event lets you know what is changing before it physically is changed. 通过ItemCheck事件,您可以在实际更改之前知道更改的内容。

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
    if (e.NewValue == CheckState.Checked)
    {
        MessageBox.Show(checkedListBox1.Items[e.Index].ToString());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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