C#在方法中调用方法时是否可以将EventArgs转换为FormClosingEventArgs?

C# Is it possible to convert EventArgs to FormClosingEventArgs when calling method in a method?

我在按下 X 时创建了一个表单关闭事件,但我也希望 'Exit' 按钮调用相同的方法,但每次我更改内容时它都会给我带来错误。

--- 下面这段代码是表单关闭事件---

// if user pressed 'Exit' button or red cross
private void TempConverterForm1_FormClosing(object sender, FormClosingEventArgs e) {
    DialogResult exitdialog = MessageBox.Show("Are you sure you want to quit?", "Quit?", MessageBoxButtons.YesNoCancel);

    if (exitdialog == DialogResult.Yes) {
        e.Cancel = false;
    }

    else {
        e.Cancel = true;
    }
}

--- 下面这段代码是我要解决的代码---

// if the 'exit' button is pressed
private void btn_Exit_Click(object sender, EventArgs e) {
    TempConverterForm1_FormClosing(sender, (FormClosingEventArgs) e);
}

我试过先不使用 FormClosingEventArgs,但它本身说 EventArgs 无法转换为关闭事件。我输入 FormClosingEventArgs 但现在它尝试从 MouseEventArgs 转换为 FormClosingEventArgs 即使我涉及按钮点击而不是鼠标点击。 我尝试进行研究,但问题反复出现并出现不同的错误消息,我迷路了并决定需要帮助。

只需在 btn_Exit_Click 中执行 this.Close()。这将使用正确的参数正确触发 Form_Closing,您的取消仍然有效。

private void btn_Exit_Click(object sender, EventArgs e)
{
       this.Close();
}