[英]C# Is it possible to convert EventArgs to FormClosingEventArgs when calling method in a method?
當按下 X 時,我創建了一個表單關閉事件,但我也希望“退出”按鈕調用相同的方法,但每次更改內容時都會出現錯誤。
--- 下面這段代碼是表單關閉事件 ---
// 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()。 這將使用正確的 arguments 正確觸發 Form_Closing,並且您的取消仍然有效。
private void btn_Exit_Click(object sender, EventArgs e)
{
this.Close();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.