form1 在单击 X 时不关闭



我遇到了一个小问题。。我需要在Visual Studio中停止它更新:

Form1不再关闭。。

在我的Form1中,我有一个代码,我在其中创建Form2-Dim f as New Fomr2的实例,但当我按下它的X按钮时,它会关闭,我无法再次调用它,因为它的disposed可以处理它。我做了这个代码。。

Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    e.Cancel = True
    Me.Hide()
End Sub

但是在添加了上面的代码之后,神奇的事情发生了。。

我无法关闭Form1

我已经在Form1_FormClosing事件上做了Application.Exit()Me.Close,但它没有开火。这让我很恼火。哈哈

只需将可关闭设置为假

Public Class Form2
    Inherits System.Windows.Forms.Form
    Public Property closable As Boolean
    Protected Overrides Sub OnClosing(ByVal e As System.ComponentModel.CancelEventArgs)
        If closable = True Then 
            MyBase.OnClosing(e)
            Return 
        End If
        e.Cancel = True
        Me.Hide() 
    End Sub
End Class

对于用法,你可以使用这种方式。每当你想让你的form2出现时,就称之为:

'inside your form1
Public  Sub  ShowForm2()
    Static f As New Form2
    f.closable = False 'it cant be true otherwise you will get exception on next call
    f.Show()
End Sub

您是如何调用form2的?

并移除e。Cancel=True将其更改为false。如果"取消"设置为true,则说明VB.net取消正在关闭的表单。。

这是seleton

form1 ...
    dim f2 as form2 = new form2
    private sub showButton()
        f2.shwo()
    end sub
    private sub DisposeF2()
        f2.ForceCLose = true
    end sub
end...
form2....
    public ForceCLose as boolean = false
    Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
                    if not ForceClose then  
            e.Cancel = True
            Me.Hide()
    end if
    End Sub
end...

最新更新