Me.FormClosing适用于某些形式但不适用于其他形式(Me.FormClosing works on some forms but not others)

编程入门 行业动态 更新时间:2024-10-27 01:22:17
Me.FormClosing适用于某些形式但不适用于其他形式(Me.FormClosing works on some forms but not others)

我希望在我的代码中处理单击表单右上角的红色X的情况。 为此,我咨询了这个并创建了一个事件处理程序: -

Private Sub DoFoo (sender As System.Object, e As System.EventArgs) _ Handles Me.FormClosing ' Do things End Sub

但是我发现(从设置断点)在某些表单上,当点击红色X时不会调用此事件处理程序,而在其他表单上则不会。 表单都是System.Windows.Forms.Form类型,但在大多数方面自然是不同的。 有谁知道可能导致这个问题以及该怎么办?

编辑

在回答Vitor的问题时,创建了无效的表格: -

If my_form Is Nothing Then my_form = New MyForm(parameters) my_form.Title = "Contour Plot Options" Else my_form.BringToFront End If my_form.Show

行为符合预期的那些是这样创建的: -

If my_working_form Is Nothing Then my_working_form = New MyWorkingForm End If my_working_form.Show

我无法在任何地方看到任何Visible属性设置或清除。

I wish in my code to handle the case when the red X at the upper right of the form is clicked. To this end I consulted this and created an event handler thus:-

Private Sub DoFoo (sender As System.Object, e As System.EventArgs) _ Handles Me.FormClosing ' Do things End Sub

but I have found (from setting breakpoints) that on certain forms this event handler is not invoked when the red X is clicked, whereas on others it is. The forms are all of type System.Windows.Forms.Form but naturally are different in most respects. Does anyone know what might be causing this and what to do about it?

Edit

In answer to Vitor's question, the form that isn't working is created thus:-

If my_form Is Nothing Then my_form = New MyForm(parameters) my_form.Title = "Contour Plot Options" Else my_form.BringToFront End If my_form.Show

Those that are behaving as expected are created like this:-

If my_working_form Is Nothing Then my_working_form = New MyWorkingForm End If my_working_form.Show

I can't see any Visible property to set or clear anywhere.

最满意答案

你的参数不太对劲。 FormClosing事件具有FormClosingEventArgs参数:

Private Sub DoFoo(ByVal sender As Object, ByVal e As FormClosingEventArgs) _ Handles Me.FormClosing     If (e.CloseReason = CloseReason.UserClosing) Then     End If End Sub

您可以检查属性“CloseReason”的e变量,该属性包括UserClosing枚举,这意味着用户关闭了表单。

每个表单都应该处理自己的FormClosing事件。 而不是订阅该事件,我发现最好像这样覆盖它:

Protected Overrides Sub OnFormClosing(ByVal e As FormClosingEventArgs) If (e.CloseReason = CloseReason.UserClosing) Then End If MyBase.OnFormClosing(e) End Sub

Your parameters aren't quite right. A FormClosing event has a FormClosingEventArgs argument:

Private Sub DoFoo(ByVal sender As Object, ByVal e As FormClosingEventArgs) _ Handles Me.FormClosing     If (e.CloseReason = CloseReason.UserClosing) Then     End If End Sub

You can inspect the e variable for the property `CloseReason', which would include a UserClosing enum, which means the user closed the form.

Each form should handle its own FormClosing event. Instead of subscribing to the event, I find it better to just override it like this:

Protected Overrides Sub OnFormClosing(ByVal e As FormClosingEventArgs) If (e.CloseReason = CloseReason.UserClosing) Then End If MyBase.OnFormClosing(e) End Sub

更多推荐

本文发布于:2023-07-22 19:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222721.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:适用于   形式   但不   FormClosing   works

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!