一个项目被选中后的CheckedListBox事件触发? [英] Which CheckedListBox event triggers after a item is checked?

查看:1862
本文介绍了一个项目被选中后的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());

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

另一个例子是,以确定是否征收将是空后此项目是(非)检查:

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天全站免登陆