C#:form.Close方法的FormClosing事件,CloseReason事件的说法。自定义设置CloseReason? [英] C#: form.Close method, FormClosing event, and CloseReason event argument. Set custom CloseReason?

查看:1306
本文介绍了C#:form.Close方法的FormClosing事件,CloseReason事件的说法。自定义设置CloseReason?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#工作为基础的工具,它利用了的FormClosing事件,而该事件应该取决于是否形式是通过编程form.Close封闭,做不同的事情();法,或者通过任何其他(用户点击X,程序退出等)

I am working on a C#-based utility that makes use of the FormClosing event, and the event is supposed to do different things depending on whether the form was closed programatically through the form.Close(); method, or by anything else (user clicking the X, program exiting, etc.)

在的FormClosing事件的FormClosingEventArgs有一个称为CloseReason属性(枚举类型CloseReason的)

The FormClosingEventArgs in the FormClosing event have a property called CloseReason (of enum type CloseReason).

CloseReason可能是:无,WindowShutDown,MdiFormClosing,UserClosing,TaskManagerClosing,FormOwnerClosing,ApplicationExitCall

CloseReason could be: None, WindowShutDown, MdiFormClosing, UserClosing, TaskManagerClosing, FormOwnerClosing, ApplicationExitCall.

在理想情况下,将有,当用户点击红色X来区分的一种方式,并且当关闭();方法被调用(通过继续按钮的点击执行其它动作之后)。然而,在FormClosingEventArgs的CloseReason属性被设置为在这两种情况下UserClosing,所以没有办法,当用户intentially关闭的形式,并且当表单编程关闭之间进行区分。这又违背了我的意料,如果关闭()方法调用任意将CloseReason等于无。

Ideally, there would be a way to distinguish between when the user clicks the red X, and when the Close(); method is called (through the clicking of a Continue button after other actions are performed). However, the CloseReason property in FormClosingEventArgs is set to UserClosing in both cases, so there is no way to distinguish between when the user closes the form intentially, and when the form is programmatically closed. This goes contrary to my expectation that CloseReason would equal None if the Close() method is invoked arbitrarily.

    //GuideSlideReturning is an cancelable event that gets fired whenever the current "slide"-form does something to finish, be it the user clicking the Continue button or the user clicking the red X to close the window. GuideSlideReturningEventArgs contains a Result field of type GuideSlideResult, that indicates what finalizing action was performed (e.g. continue, window-close)

    private void continueButton_Click(object sender, EventArgs e)
    { //handles click of Continue button
        GuideSlideReturningEventArgs eventArgs = new GuideSlideReturningEventArgs(GuideSlideResult.Continue);
        GuideSlideReturning(this, eventArgs);
        if (!eventArgs.Cancel)
            this.Close();
    }

    private void SingleFileSelectForm_FormClosing(object sender, FormClosingEventArgs e)
    { //handles FormClosing event
        if (e.CloseReason == CloseReason.None)
            return;
        GuideSlideReturningEventArgs eventArgs = new GuideSlideReturningEventArgs(GuideSlideResult.Cancel);
        GuideSlideReturning(this, eventArgs);
        e.Cancel = eventArgs.Cancel;
    }



这样做的问题是,当关闭();方法是在事件GuideSlideReturning饰面不被取消之后调用,所述的FormClosing事件处理程序是无法分辨的形式通过该方法,而不是由用户被关闭关闭。

The issue with this is that when the Close(); method is invoked after the event GuideSlideReturning finishes without being canceled, the FormClosing event handler is unable to tell that the form was closed through the method instead of being closed by the user.

什么是理想的是,如果我可以定义的FormClosing事件的FormClosingEventArgs CloseReason是什么,是这样的:

What would be ideal is if I could define what the FormClosing event's FormClosingEventArgs CloseReason would be, like this:

    this.Close(CloseReason.None);



有没有办法做到这一点?所述form.Close();方法没有接受任何参数的重载,所以有一个变量我可以设置或另一种方法,我可以打电话?

Is there a way to do this? The form.Close(); method does not have any overloads that accept any parameters, so is there a variable I can set or an alternate method I can call?

推荐答案

调用close编程之前设置一个标志。这可以在一个私有方法来包裹起来:

Set a flag before calling close programmatically. This can be wrapped up in a private method:

private bool _programmaticClose;

// Call this instead of calling Close()
private void ShutDown()
{
    _programmaticClose = true;
    Close();
}  

protected override void OnFormClosing(FormClosingEventArgs e)
{
    base.OnFormClosing();
    _programmaticClose = false;
}

这篇关于C#:form.Close方法的FormClosing事件,CloseReason事件的说法。自定义设置CloseReason?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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