当用户单击 X(关闭程序)时,VB.NET 重载默认功能
当用户在基于 VB.NET 表单的应用程序中单击 X 时,如何覆盖默认功能?
我当前正在处理 MyBase.Closing 事件...但由于它是 DirectX 应用程序,因此我需要在允许表单关闭之前进行一些清理。
How do I go about overriding the default functionality when a user clicks the X in a VB.NET form-based application?
I am currently handling the MyBase.Closing event... but since it's a DirectX app, I need to do some cleanup before allowing the form the close.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想在表单关闭之前进行一些额外的清理,请在表单的 表单关闭事件。
此事件还允许您通过事件的 FormClosingEventArgs.Cancel 属性。该事件的 CloseReason 属性将告诉您您为什么调用该事件(您需要检查 CloseReason.UserClosing)
If you want to do some extra clean up before a form closes, do so in the form's Form Closing event.
This event also allows you to cancel the form close through the event's FormClosingEventArgs.Cancel property. The event's CloseReason property will tell you why the event was called (you'd check for CloseReason.UserClosing)
回复您的其他评论;在事件触发之前做任何事情都为时过早;毕竟,其他一些代码可能会取消关闭,并且您将留下一个失败的表单。就我个人而言,我很想重写
OnClosed
;不太懂 VB,但是用 C# 写的:Re your other comments; doing anything before the event has fired would be premature; after all, some other code might cancel the close, and you'd be left with a scuppered form. Personally, I'd be tempted to override
OnClosed
; not much of a VB head, but in C#:如果您不愿意将代码放入 Form_Closing 事件中,我知道的唯一其他选项是我使用过一两次的“hack”。没有必要诉诸此技巧,但事实是:
不要使用正常的关闭按钮。相反,创建您的窗体,使其没有 ControlBox。您可以通过在表单上设置 ControlBox = false 来完成此操作,在这种情况下,表单顶部仍然会有普通栏,或者您可以将表单的 FormBorderStyle 设置为“None”。如果您采用第二条路线,则会出现顶部不会有横条,也不会出现任何其他可见的边框,因此您必须通过在窗体上绘图或通过面板控件的艺术使用来模拟它们,
然后您可以添加一个标准按钮并将其设为。 看起来像一个关闭按钮,并将清理代码放入其中。在按钮事件结束时,只需调用
this.Close()
(。 C#) 或Me.Close()
(VB)If you don't feel comfortable putting your code in the Form_Closing event, the only other option I am aware of is a "hack" that I've used once or twice. It should not be necessary to resort to this hack, but here it is:
Don't use the normal close button. Instead, create your form so that it has no ControlBox. You can do this by setting ControlBox = false on the form, in which case, you will still have the normal bar across the top of the form, or you can set the form's FormBorderStyle to "None. If you go this second route, there will be no bar across the top, or any other visible border, so you'll have to simulate those either by drawing on the form, or by artistic use of Panel controls.
Then you can add a standard button and make it look like a close button, and put your clean-up code in there. At the end of the button event, just call
this.Close()
(C#) orMe.Close()
(VB)