以编程方式设置 closereason [英] Setting programmatically closereason

查看:45
本文介绍了以编程方式设置 closereason的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在表单内部调用 This.Close() 后设置表单的 CloseReason.

I want to set the CloseReason of a form after I call This.Close() inside the form.

通常,此表单通过调用 This.Close() 自行关闭,但我想询问用户是否真的想关闭表单,并发送带有一些信息的 mbox.但我有这个:

Usually, this forms is closed by itself calling This.Close(), but I want to ask the user if they REALLY want to close the form, and send a mbox with some info. But I have this:

protected override void OnFormClosing(FormClosingEventArgs e)
    {
        if (e.CloseReason == CloseReason.UserClosing)
        {
            if (MessageBox.Show("¿Desea Salir realmente?\nLa factura aun no ha sido pagada por lo que volverá a la pantalla anterior y podrá seguir agregando productos") == DialogResult.No)
            {
                e.Cancel = true;
            }
        }
        base.OnFormClosing(e);
    }

但是每次我调用This.Close();CloseReason 总是 UserClosing.

But every time I call This.Close(); the CloseReason is always UserClosing.

我可以在调用后设置它还是我必须处理不同的 OnFormClosing?

Can I set it after the call or I have to handle the OnFormClosing different?

推荐答案

我不认为你可以这样做,我总是做的是使用标志

I don't think you can do that, what i always do is to use a flag

appClosing = true;
this.Close();

然后检查:

protected override void OnFormClosing(FormClosingEventArgs e)
    {
        if (e.CloseReason == CloseReason.UserClosing && !appClosing)
        {
            if (MessageBox.Show("¿Desea Salir realmente?\nLa factura aun no ha sido pagada por lo que volverá a la pantalla anterior y podrá seguir agregando productos") == DialogResult.No)
            {
                e.Cancel = true;
            }
        }
        base.OnFormClosing(e);
    }

这篇关于以编程方式设置 closereason的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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