不想退出Windows应用程序? [英] dont want to exit a windows application??
问题描述
我在VS 2008中创建了一个简单的Windows应用程序.我不希望在用户按下退出按钮[右上角的十字按钮]时关闭该应用程序.有可能吗?如果是,请详细告诉我编码.
i created a simple windows application in VS 2008.i don''t want the application to close when the user presses the exit button[cross button on right hand top corner].Is it possible?? if yes please tell me the coding in detail.
推荐答案
首先订阅表单的关闭事件.之后,您可以在事件处理程序中取消关闭操作:
First subscribe to the Form''s Closing event. After that in the event handler you can cancel the close:
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// Cancel the close action
e.Cancel = true;
}
但是仅此代码将阻止您关闭应用程序!为了提供一种关闭表单的方法,您可以使用一个标志来确定是否应该取消关闭事件.
我希望这有帮助. :)
But this code alone will prevent you from ever closing the application! In order to provide a way of closing the form, you can use a flag to determine whether or not the closing event should be cancelled.
I hope this helps. :)
如果您不担心丢失其他控制按钮(最小化/还原/最大化),可以将Forms ControlBox属性设置为false,那么就不会按下退出按钮.
If you aren''t worried about losing the other control buttons (minimise/restore/maximise) you could set the Forms ControlBox property to false, then there wouldn''t be an exit button to press.
this.ControlBox = false;
调用InitializeComponent()后,最好在表单构造函数中完成.要么在设计器中使用属性"窗口完成.
Probably best done in the forms constructor after InitializeComponent() is called. Either that or done in the Designer using the Properties window.
这篇关于不想退出Windows应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!