如何通過單擊X按鈕或(this.Close())找出表單是否關閉?C#Form X Button Clicked
3
A
回答
18
表單具有帶FormClosing類型的參數的事件FormClosingEventArgs。
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
if (MessageBox.Show(this, "Really?", "Closing...",
MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
== DialogResult.Cancel) e.Cancel = true;
}
}
+0
我不想問是否真的關閉。我的表單有一個取消按鈕,並點擊取消我設置爲null將返回一個字段。從外面看,我知道當這個表單返回null時我不必做點什麼。但是當通過單擊X關閉窗體時,該字段不爲空,因此外部代碼崩潰。 – 2010-09-17 14:09:20
+1
技術上他確實回答你的問題...... – Xander 2010-09-17 14:58:22
2
你可以一起刪除'X'嗎?
一種形式的屬性是如果你想設置的返回字段設置爲null,像你一樣,當你點擊你的表格取消「控制盒」只是將其設置爲false
1
:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
returnfield = null;
this.close();
}
}
0
對於OnFormClosing
的FormClosingEventArgs.CloseReason
是UserClosing
要麼「X」按鈕或form.Close()
方法。 我的解決方案:
//override the OnFormClosing event
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.ApplicationExitCall)// the reason that you need
base.OnFormClosing(e);
else e.Cancel = true; // cancel if the close reason is not the expected one
}
//create a new method that allows to handle the close reasons
public void closeForm(FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing) this.Close();
else e.Cancel = true;
}
//if you want to close the form or deny the X button action invoke closeForm method
myForm.closeForm(new FormClosingEventArgs(CloseReason.ApplicationExitCall, false));
//the reason that you want ↑
在這個例子中的關閉(X)按鈕不關閉窗體
相關問題
- 1. jquerymobile button clicked goes blue
- 2. c#「return」不會終止Button Clicked事件並觸發定時器
- 3. x-www-form-urlencoded objective c
- 4. Wicket AjaxFallbackButton in simple form(update:also Button)
- 5. Python post form with button javascript
- 6. html post form to a.php in case of button one,post form to b.php case of button two
- 7. x-editable form with typehead
- 8. 如何在android studio的If語句中編寫「if button clicked」?
- 9. Hello Views/Form Stuff/Custon Button教程錯誤
- 10. Form Helper X Form Builder。有什麼不同?
- 11. clicked()按鈕信號
- 12. QGroupBox clicked/focus
- 13. Clicked out event TextBox
- 14. Double/Float TO Rational(X/Y Form)
- 15. clicked()QListView的信號
- 16. Button button =(Button)v;和Button button =(Button)findviewbyid(r.id.button1);
- 17. X clicked/alt-F4和windowsclosing()事件之間的事件
- 18. jquery .clicked不起作用
- 19. 如何在Submit Button URL中包含Acrobat Form字段值鏈接
- 20. unfocusable form C#
- 21. Android的是,當(長)-clicked
- 22. 綁定Enter Button to Button(PyGObject)
- 23. multipart/form-data和application-x-www-form-urlencoded有什麼區別?
- 24. MouseListener not catch the mousePressed/Clicked
- 25. Add Class to clicked li object
- 26. 「ng-class」on each clicked item
- 27. HTML <button> onclick不工作<form>標記
- 28. 渲染帶有內容且沒有標籤的Zend \ Form \ Element \ Button
- 29. 如何從<form>和<button>的onclick刪除
- 30. Redux Form,Radio Button Fields,如何支持變量值?
這個比爲它編寫的代碼更簡單。 – 2012-12-29 23:36:52