确定谁触发的事件 [英] Determine who fired an event

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

问题描述

背景:

在我的WinForms的形式,我有一个经过的ListView 和大师复选框所谓的 checkBoxAll
主的行为如下:

In my winforms form, I have a Checked ListView and a "master" checkbox called checkBoxAll. The behaviour of the master is as follows:


  • 如果主被选中或取消选中,所有ListViewItems必须改变因此。

  • If the master is checked or unchecked, all ListViewItems must change accordingly.

如果用户取消选中一个ListViewItem的,主机必须相应地改变。

If the user unchecks a ListViewItem, the master must change accordingly.

如果用户检查一个ListViewItem的,和所有其他ListViewItems是藏汉检查,主机必须相应地改变。

If the user checks a ListViewItem, and all other ListViewItems are checked aswell, the master must change accordingly.

我写了下面的代码来模仿这种行为:

I have written the following code to mimic this behaviour:

private bool byProgram = false; //Flag to determine the caller of the code. True for program, false for user.

private void checkBoxAll_CheckedChanged(object sender, EventArgs e)
{
    //Check if the user raised this event.
     if (!byProgram)
     {
         //Event was raised by user!

         //If checkBoxAll is checked, all listviewitems must be checked too and vice versa.

         //Check if there are any items to (un)check.
         if (myListView.Items.Count > 0)
         {
             byProgram = true; //Raise flag.

             //(Un)check every item.
             foreach (ListViewItem lvi in myListView.Items)
             {
                 lvi.Checked = checkBoxAll.Checked;
             }

             byProgram = false; //Lower flag.
         }
     }
}

private void myListView_ItemChecked(object sender, ItemCheckedEventArgs e)
{
    //Get the appropiate ListView that raised this event
    var listView = sender as ListView;

    //Check if the user raised this event.
    if (!byProgram)
    {
        //Event was raised by user!

        //If all items are checked, set checkBoxAll checked, else: uncheck him!

        bool allChecked = true; //This boolean will be used to set the value of checkBoxAll

        //This event was raised by an ListViewItem so we don't have to check if any exist. 
        //Check all items untill one is not checked.
        foreach (ListViewItem lvi in listView.Items)
        {
            allChecked = lvi.Checked;
            if (!allChecked) break;
        }

        byProgram = true; //Raise flag.

        //Set the checkBoxAll according to the value determined for allChecked.
        checkBoxAll.Checked = allChecked;

        byProgram = false; //Lower flag.
    }
}

在这个例子中,我使用了一个标志( byProgram ),以确保一个事件是由用户或引起不,从而防止无限循环(一个事件可以触发另一,其可再次触发第一个等等,等等)。 IMHO,这是一个哈克溶液。
我搜索周围,但我找不到一个MSDN文档的方法来确定一个用户控件事件直接解雇感谢给用户。这令我奇怪的(再次,恕我直言)。

In this example, I use a flag (byProgram) to make sure an event was caused by the user or not, thereby preventing an infinite loop (one event can fire another, which can fire the first one again etc. etc.). IMHO, this is a hacky solution. I searched around but I couldn't find a MSDN documented method to determine if an User Control Event was directly fired thanks to the user. Which strikes me as odd (again, IMHO).

我知道FormClosingEventArgs有,我们可以用它来确定用户是否关闭窗体或不是现场。但据我所知,这是提供这种功能的唯一EventArg ...

I know that the FormClosingEventArgs has a field which we can use to determine if the user is closing the form or not. But as far as I know, that is the only EventArg that provides this kind of functionality...

因此,在总结:

有没有办法(比我的例子除外),以确定事件是由用户直接开除?

请注意:我并不是一个事件的发送者!这并不重要,如果我的代码someCheckBox.Checked = TRUE;或手动设置someCheckBox,事件的发送方将始终someCheckBox。我想找出是否有可能以确定它是否是通过用户(点击),或者通过程序(.Checked = TRUE)。

Please note: I don't mean the sender of an event! It won't matter if I code someCheckBox.Checked = true; or manually set someCheckBox, the sender of the event will always be someCheckBox. I want to find out if it is possible to determine whether it was through the user (click) or by the program (.Checked = true).

Aaand也:的花来写这个问题,30%的时间是正确制定问题和称号。仍然不知道,如果它是一个100%的清楚,所以,如果你认为你可以做,请编辑好:)

Aaand also: 30% of the time it took to write this question was to formulate the question and the title correctly. Still not sure if it is a 100% clear so please edit if you think you can do better :)

推荐答案

这是什么我遇到了很多和我倾向于尝试做VS程序交互用户之间的互动是不是分裂它是什么 - 我用比较通用的代码,即用户界面正在更新,并且不需要进行处理任何事件。我通常是通过的BeginUpdate / EndUpdate 方法,例如

This is something I come across quite a lot and what I tend to try do is not split it between user interaction vs program interaction - I use more generic code i.e. the UI is being updated and doesn't require any events to be handled. I usually package this up through BeginUpdate/EndUpdate methods e.g.

private int updates = 0;

public bool Updating { get { return updates > 0; } }

public void BeginUpdate()
{
    updates++;
}

public void EndUpdate()
{
    updates--;
}

public void IndividualCheckBoxChanged(...)
{
    if (!Updating)
    {
        // run code
    }
}

public void CheckAllChanged(...)
{
    BeginUpdate();
    try
    {
        // run code
    }
    finally
    {
        EndUpdate();
    }
}

这篇关于确定谁触发的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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