我希望在我的代码中处理单击表单右上角的红色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 Subbut 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.ShowThose 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.ShowI 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 SubYour 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 SubYou 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更多推荐
发布评论