C#中取消事件的问题 [英] problem with cancel events in C#

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

问题描述

为什么在此代码中,关闭事件没有取消,而我却取消了它,而FormClosing却被正确取消了?

why in this code Closing Event not canceled ,while i cancel it .and FormClosing Canceled Corrrectly?

public partial class Form1 : Form
{
    bool isDataSaved;
    event CancelEventHandler Closing;
    public Form1()
    {
        InitializeComponent();
        OtherInitialize();

    }

    // Call this method from the constructor of your form
    private void OtherInitialize()
    {
        this.Closing += new CancelEventHandler(this.Form1_Closing);
        // Exchange commented line and note the difference.this.isDataSaved = true;
        this.isDataSaved = false;
    }

    private void Form1_Closing(Object sender, CancelEventArgs e)
    {
        if (!isDataSaved)
        {
            e.Cancel = true;
            label1.Text = "test";
            MessageBox.Show("You must save first.");

        }
        else
        {
            e.Cancel = false;

            MessageBox.Show("Goodbye.");
        }
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        label1.Text = "jjf";
        Closing(this,new CancelEventArgs() );
    }

}

推荐答案

它不起作用,因为您在FormClosing事件处理程序中发出了关闭信号,但没有使用事件提供的结果.您可以检查CancelEventArgs.Cancel属性并从中设置FormClosingEventArgs.Cancel属性,但是我很困惑为什么根本创建了一个新事件...
It doesn''t work because you signal Closing in your FormClosing event handler, but don''t use the result that your event provides. You could check the CancelEventArgs.Cancel property and set the FormClosingEventArgs.Cancel property from that, but I''m puzzled as to why you have created a new event at all...


Don不能从您的FormClosing事件处理程序中调用Closing.
您有2个选择:

1-事件处理:
Don''t call Closing from your FormClosing event handler.
You have 2 options:

1- event handling:
public partial class Form1 : Form
{
    bool isDataSaved;
    public Form1()
    {
        InitializeComponent();
        OtherInitialize();
    }
    private void OtherInitialize()
    {
        this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
        // Exchange commented line and note the difference.
        //this.isDataSaved = true;
        this.isDataSaved = false;
    }
    void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (e.CloseReason != CloseReason.UserClosing)
            return;

        e.Cancel = !isDataSaved;
    }


2-覆盖:


2- overriding:

public partial class Form1 : Form
{
    bool isDataSaved;
    public Form1()
    {
        InitializeComponent();
        OtherInitialize();
    }
    private void OtherInitialize()
    {
        // Exchange commented line and note the difference.
        //this.isDataSaved = true;
        this.isDataSaved = false;
    }

    protected override void OnFormClosing(FormClosingEventArgs e)
    {
        if (e.CloseReason == CloseReason.UserClosing && !isDataSaved)
            e.Cancel = true;
        base.OnFormClosing(e);
    }


这篇关于C#中取消事件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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