"e.Cancel"如何导致该事件被取消 [英] "e.Cancel" how causes that event canceled

查看:144
本文介绍了"e.Cancel"如何导致该事件被取消的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码中的"e.Cancel"如何导致该事件被取消.后台发生了什么?

in this code "e.Cancel" how causes that event canceled.what happen in the background?

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        protected override void OnClosing(CancelEventArgs e)
        {
            e.Cancel = true;    //how do this work??

        }
     
    }

推荐答案

首先,MSDN指出OnClosing是过时的,应改为使用OnFormClosing.


其次,当您决定重写方法时,请不要忘记调用基本成员(否则该表单将记录触发事件):
First of all, MSDN states that OnClosing is obsolote and you should use OnFormClosing instead.


Second, when you decide to override a method, don''t forget to call the base member (otherwise the form will note fire the event):
protected override void OnClosing(CancelEventArgs e)
{
     //do whatever you want
     ...

     //don''t forget to call the base class
     base.OnClosing(e)
}



e.Cancel设置为true后,事件调用者(窗体)将检查该值,并决定是否对事件进行处理(如果为true)(否则,则不对其进行处理).在您的情况下,如果将值设置为false,则基本方法OnClosing将不会关闭表单.



After you set e.Cancel to true, the event caller (the form) will check that value and decide to process the event if it is true (or not to process it if it false). In your case, if you set the value to false, then the base method OnClosing will not close the form.


正如Olivier为您所说的那样:首先,MSDN指出OnClosing是过时的,您应该改用OnFormClosing."

最重要的是,OnFormClosing是事件处理程序.设置e.Cancel不会触发新事件,而是在事件参数中设置一个布尔属性.当事件使用"base.OnClosing(e)"传递给基类时,在继承结构中的某处将有代码读取该属性,然后如果该值为true,则取消事件处理.
As Olivier already stated for you, "First of all, MSDN states that OnClosing is obsolote and you should use OnFormClosing instead."

On top of that, the OnFormClosing is the event handler. Setting e.Cancel does not fire a new event, but rather sets a boolean property in the event arguments. When the event gets passed on to the base class using "base.OnClosing(e)", somewhere in the inheritance structure there would be code to read that property and then cancel the event processes if the value is true.


---已删除-并推荐SAKryukov的解决方案.

我在这里更新的答案是一个e.cancel如何与自定义示例一起工作的示例.

这是带有自定义事件的类,该类的自变量类型为CancelEventArgs.

触发此事件时,可以设置其cancel属性,该属性向对象发出有关进一步操作的信号.

---deleted-- and recommend SAKryukov''s solution.

And my updated answer here is an example how the e.cancel works with a custom example.

Here is a class with a custom event which has the CancelEventArgs type of argument.

When this event fires you can set its cancel property which signals the object about further action.

public class PersonWithEvent
{
    public event CancelEventHandler NameAcceptanceEvent;
    private string _name;
    public string ErrMessage{get;set;}
    public string Name
    {
        get { return _name; }
        set
        {
                CancelEventArgs cancelParams = new CancelEventArgs();
                NameAcceptanceEvent.DynamicInvoke(new object[] { this, cancelParams });
                if (cancelParams.Cancel != true)
                {
                    _name = value;
                    ErrMessage = "";
                }
                else
                {
                    ErrMessage = "This name not allowed";
                }
        }
    }

}



现在,名称的接受并不封装在该对象中,而是委托给可以实现自己的逻辑的用户.



Now the acceptance of a name is not encapsulated in this object, but delegated to the user who can implement his own logic for that.

private string personName;
 private void button1_Click(object sender, EventArgs e)
{
    PersonWithEvent person = new PersonWithEvent();
    person.NameAcceptanceEvent+=new CancelEventHandler(person_NameAcceptanceEvent);
    personName = "Monster";
    person.Name = personName;
    MessageBox.Show(person.Name);
    MessageBox.Show(person.ErrMessage);
    personName = "Angel";
    person.Name = personName;
    MessageBox.Show(person.Name);
    MessageBox.Show(person.ErrMessage);
}
private void person_NameAcceptanceEvent(object sender, CancelEventArgs e)
{
    if (personName == "Monster")
    {
        e.Cancel = true;
    }
}





祝你好运





Good luck


这篇关于"e.Cancel"如何导致该事件被取消的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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