Me.FormClosing 适用于某些表格,但不适用于其他表格 [英] Me.FormClosing works on some forms but not others

查看:38
本文介绍了Me.FormClosing 适用于某些表格,但不适用于其他表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

但我发现(通过设置断点)在某些表单上,单击红色 X 时不会调用此事件处理程序,而在其他表单上则是.表单都是 System.Windows.Forms.Form 类型,但在大多数方面自然是不同的.有谁知道可能导致这种情况的原因以及如何处理?

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?

编辑

为了回答 Vitor 的问题,创建了不起作用的表单:-

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

我看不到任何要在任何地方设置或清除的 Visible 属性.

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

推荐答案

您的参数不太正确.FormClosing 事件有一个 FormClosingEventArgs 参数:

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

您可以检查属性 `CloseReason' 的 e 变量,它包含一个 UserClosing 枚举,这意味着用户关闭了表单.

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

每个表单都应该处理自己的 FormClosing 事件.我发现与其订阅事件,不如像这样覆盖它:

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

这篇关于Me.FormClosing 适用于某些表格,但不适用于其他表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆