单击C#表单X按钮
如何通过单击X按钮或(this.Close())来确定表单是否已关闭?
该窗体具有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; } }
你可以完全删除’X’吗?
表单的一个属性是“ControlBox”,只是将其设置为false
如果要将返回的字段设置为null,就像在表单中单击“取消”时所做的那样:
private void Form1_FormClosing( object sender, FormClosingEventArgs e ) { if ( e.CloseReason == CloseReason.UserClosing ) { returnfield = null; this.close(); } }
对于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)按钮不会关闭表单
上述就是C#学习教程:单击C#表单X按钮分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/954864.html