如何通過單擊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. Python post form with button javascript
- 5. Wicket AjaxFallbackButton in simple form(update:also Button)
- 6. x-editable form with typehead
- 7. Hello Views/Form Stuff/Custon Button教程錯誤
- 8. Form Helper X Form Builder。有什麼不同?
- 9. clicked()按鈕信號
- 10. Clicked out event TextBox
- 11. X clicked/alt-F4和windowsclosing()事件之間的事件
- 12. jquery .clicked不起作用
- 13. 綁定Enter Button to Button(PyGObject)
- 14. multipart/form-data和application-x-www-form-urlencoded有什麼區別?
- 15. HTML <button> onclick不工作<form>標記
- 16. 如何從<form>和<button>的onclick刪除
- 17. `min-width`不能在form`input [type =「button」]`元素上工作嗎?
- 18. Redux Form,Radio Button Fields,如何支持變量值?
- 19. 「ng-class」on each clicked item
- 20. 訪問angularjs中的clicked li元素
- 21. Jquery on BUTTON
- 22. C#form numeric up down
- 23. Button Button Margin
- 24. C#WPF Mouseover Button - 無內容
- 25. Ajax請求:application/x-www-form-urlencoded params
- 26. Spring MVC和@RequestParam與x-www-form-urlencoded
- 27. angular2-rc4-當x-www-form-urlencoded給出
- 28. AngularJS發送請求x-www-form-urlencoded
- 29. 錯誤415:x-www-form-urlencoded與JSON
- 30. Weebly button broken
這個比爲它編寫的代碼更簡單。 – 2012-12-29 23:36:52