一次只能检查一个ListViewItem [英] Check only one ListViewItem at time

查看:140
本文介绍了一次只能检查一个ListViewItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Compact Framework开发智能设备项目。



我有一个 ListView 可以查看 ListViewItem s:属性 CheckBoxes 是true。我只需要检查一个 ListViewItem ,所以我订阅了 ItemCheck 事件:

  //我需要知道最后一个项目
private ListViewItem lastItemChecked;

private void listView_ItemCheck(object sender,ItemCheckEventArgs e)
{
if(lastItemChecked!= null&& lastItemChecked.Checked)
{
/ *我需要执行以下操作来防止无限递归:
即订阅然后取消订阅ItemCheck事件。 * /
listView.ItemCheck - = listView_ItemCheck;
lastItemChecked.Checked = false;
listView.ItemCheck + = listView_ItemCheck;
}

lastItemChecked = listView.Items [e.Index];
}

有没有更好的方法来阻止无限递归,因此有$ code>堆栈溢出?

解决方案

嗯,我认为可能比玩杂耍EventHandler正在检查lastItemCheck是否是EventArgs中的当前项。像这样:

  //我需要知道最后一个项目
private ListViewItem lastItemChecked;

private void listView_ItemCheck(object sender,ItemCheckEventArgs e)
{
//如果我们将lastItem设置为checked,并且它不同于
// item than如果(lastItemChecked!= null&& lastItemChecked.Checked
&& lastItemChecked!= listView.Items [e.Index])
,则取消选中该事件
{
//取消选中最后一个项目并存储新的
lastItemChecked.Checked = false;
}

//存储当前项
lastItemChecked = listView.Items [e.Index];
}

我认为你会同意,重新分配EventHandlers有点比简单检查存储对象的引用更糟糕。


I'm developing a smart device project using the Compact Framework.

I have a ListView with several checkable ListViewItems: the property CheckBoxes is true. I need to check only one ListViewItem at time, so I subscribed the ItemCheck event:

// I need to know the last item checked
private ListViewItem lastItemChecked;

private void listView_ItemCheck(object sender, ItemCheckEventArgs e)
{
    if (lastItemChecked != null && lastItemChecked.Checked)
    {
        /* I need to do the following to prevent infinite recursion:
        i.e. subscribe and then unsubscribe the ItemCheck event. */
        listView.ItemCheck -= listView_ItemCheck;
        lastItemChecked.Checked = false;
        listView.ItemCheck += listView_ItemCheck;
    }

    lastItemChecked = listView.Items[e.Index];
}

Is there a better way to prevent the infinite recursion and thus the Stack Overflow?

解决方案

Well, I think what might be better than juggling with the EventHandlers is checking if the lastItemCheck is a current item from the EventArgs. Like this:

// I need to know the last item checked
private ListViewItem lastItemChecked;

private void listView_ItemCheck(object sender, ItemCheckEventArgs e)
{
    // if we have the lastItem set as checked, and it is different
    // item than the one that fired the event, uncheck it
    if (lastItemChecked != null && lastItemChecked.Checked
        && lastItemChecked != listView.Items[e.Index] )
    {
        // uncheck the last item and store the new one
        lastItemChecked.Checked = false;
    }

    // store current item
    lastItemChecked = listView.Items[e.Index];
}

I think that you'll agree, that re-assigning EventHandlers is a bit worse than simply checking the reference of the stored object.

这篇关于一次只能检查一个ListViewItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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