-5

如何检测在此之前触发的 TextBox_Leave 事件中窗体/窗口的控制框右上角的关闭 (X) 按钮的单击事件?我不想知道 CloseReason、FormClosing、FormClosed 或类似的东西,除非它们是不可避免的。

我确实想检测用户是否单击了表单的 X 按钮。我的情况显然是我有文本框和按钮。在 TextBox_Leave 中打开了一个新屏幕,但是当 textBox 聚焦时,请先按(X)按钮离开事件触发,但我需要知道用户是否按(X)按钮或不从离开事件显示新屏幕或关闭当前屏幕.

我不能使用提到的事件,因为它会在 TextBox_Leave 之后触发但我想知道在执行 TextBox_Leave 代码之前是否按下了(X)按钮。

4

3 回答 3

2

如果我理解正确,试试这个:

bool isButtonClick = false;

private void button1_Click(object sender, EventArgs e)
{
    isButtonClick = true;
    this.Close();
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if(isButtonClick)
    {
         //User close form with Button
    }
    else
    {
         //User close form with (X) button at the top right corner of the
         //control box of a form/window
    }
}
于 2014-05-19T14:19:47.043 回答
0

您(已编辑)问题的答案是:“否”。TextBox.Leave 事件将首先触发,然后处理关闭 (X) 按钮。

但是,在不知道您为什么要翻转订单的情况下,我们无法提供有关使用此功能的更多信息。

于 2014-05-19T15:17:00.530 回答
0

我在解释您的问题时遇到了一些问题,但如果我理解正确,您想知道用户是否选择在用户关闭程序之前关闭程序,以便您可以停止任何正在进行的工作?

您是否有任何理由不想为此使用 form_closure 或者我完全误解了您的问题?因为我会像这样使用 form_closure 来解决它

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
            // Display a MsgBox asking the user to close the form.
            if (MessageBox.Show("Are you sure you want to close the form?", "Close Form",
               MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.No)
            {
                // Cancel the Closing event
                e.Cancel = true;
            }

}

消息框当然可以用代码替换,以停止您的代码当前正在执行的任何正在进行的作业。但也许我误解了你在找什么?

于 2014-05-19T14:14:08.700 回答