如何知道用户点击英寸×"或"关闭"按钮? [英] How to know user has clicked "X" or the "Close" button?

查看:243
本文介绍了如何知道用户点击英寸×"或"关闭"按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MSDN我发现 CloseReason.UserClosing 要知道,用户已决定关闭该窗体 但我想这是相同的两个单击X按钮或单击关闭按钮。 那么,怎样才能在我的code区分这两者之间?

In MSDN I found CloseReason.UserClosing to know that the user had decided to close the form but I guess it is the same for both clicking the X button or clicking the close button. So how can I differentiate between these two in my code?

谢谢大家。

推荐答案

假设你要求的WinForms,你可以使用的的FormClosing()事件。本次活动的FormClosing()被触发的任何时间的一种形式就是要关闭。

Assuming you're asking for WinForms, you may use the FormClosing() event. The event FormClosing() is triggered any time a form is to get closed.

要检测如果用户点击X或你的CloseButton,你可能会得到它通过发送对象。尝试转换发件人为按钮控制,并验证也许它的名字CloseButton,例如。

To detect if the user clicked either X or your CloseButton, you may get it through the sender object. Try to cast sender as a Button control, and verify perhaps for its name "CloseButton", for instance.

private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
    if (string.Equals((sender as Button).Name, @"CloseButton"))
        // Do something proper to CloseButton.
    else
        // Then assume that X has been clicked and act accordingly.
}

否则,我从来没有需要区分X或CloseButton是否被点击,因为我想执行特定的事件的FormClosing的东西,比如关闭所有MdiChildren关闭MDIContainerForm,或事件检查未保存的更改之前。在这些情况下,我们不需要,根据我,从任一按键进行区分。

Otherwise, I have never ever needed to differentiate whether X or CloseButton was clicked, as I wanted to perform something specific on the FormClosing event, like closing all MdiChildren before closing the MDIContainerForm, or event checking for unsaved changes. Under these circumstances, we don't need, according to me, to differentiate from either buttons.

修改按闭幕[ALT] + [F4]也将触发的FormClosing()事件,因为它发出了一个消息,说要关闭窗体。您可以通过设置取消该事件的

EDIT Closing by [ALT]+[F4] will also trigger the FormClosing() event, as it sends a message to the Form that says to close. You may cancel the event by setting the

FormClosingEventArgs.Cancel = true. 

在我们的例子中,这将转化为

In our example, this would translate to be

e.Cancel = true.

注意的FormClosing(之差),并在<一href="http://msdn.microsoft.com/en-us/library/system.windows.forms.form.closed.aspx">FormClosed()事件。

在形式收到的消息进行封闭,并确认是否有事可做,然后才关闭时的FormClosing。

FormClosing occurs when the form received the message to be closed, and verify whether it has something to do before it is closed.

FormClosed时出现的形式实际上是封闭的,因此之后被关闭。

FormClosed occurs when the form is actually closed, so after it is closed.

这是否帮助?

这篇关于如何知道用户点击英寸×&QUOT;或&QUOT;关闭&QUOT;按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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