检查项目后哪个 CheckedListBox 事件触发? [英] Which CheckedListBox event triggers after a item is checked?

查看:17
本文介绍了检查项目后哪个 CheckedListBox 事件触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 CheckedListBox,我想要一个事件 一个项目被检查后,这样我就可以使用具有新状态的 CheckedItems.

I have a CheckedListBox where I want an event after an item is checked so that I can use CheckedItems with the new state.

由于 ItemChecked 在 CheckedItems 更新之前被触发,所以它无法立即使用.

Since ItemChecked is fired before CheckedItems is updated it won't work out of the box.

当 CheckedItems 更新时,我可以使用什么样的方法或事件来通知我?

What kind of method or event can I use to be notified when the CheckedItems is updated?

推荐答案

您可以使用 ItemCheck 事件,如果您还检查正在单击的项目的新状态.这在事件参数中可用,如 e.NewValue.如果选中 NewValue,则在逻辑中包含当前项目和正确的集合:

You can use the ItemCheck event, if you also check the new state of the item which is being clicked. This is available in the event args, as e.NewValue. If NewValue is checked, include the current item along with the collection proper in your logic:

    private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
    {                     
        List<string> checkedItems = new List<string>();
        foreach (var item in checkedListBox1.CheckedItems)
            checkedItems.Add(item.ToString());

        if (e.NewValue == CheckState.Checked)
            checkedItems.Add(checkedListBox1.Items[e.Index].ToString());
        else
            checkedItems.Remove(checkedListBox1.Items[e.Index].ToString());

        foreach (string item in checkedItems)
        {
            ...
        }
    }

再举一个例子,判断这个项目被(un-)检查后集合是否为空:

As another example, to determine if the collection will be empty after this item is (un-)checked:

private void ListProjects_ItemCheck(object sender, ItemCheckEventArgs args)
{
    if (ListProjects.CheckedItems.Count == 1 && args.NewValue == CheckState.Unchecked)
        // The collection is about to be emptied: there's just one item checked, and it's being unchecked at this moment
        ...
    else
        // The collection will not be empty once this click is handled
        ...
}

这篇关于检查项目后哪个 CheckedListBox 事件触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆