单击X时,form1不会关闭

时间:2013-12-10 07:29:21

标签: vb.net winforms

我遇到了一个小问题..我需要在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

我已在Application.Exit()事件中执行了Me.CloseForm1_FormClosing,但它并未触发。这对我来说非常恼火。洛尔

3 个答案:

答案 0 :(得分:1)

将闭包设置为false

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

答案 1 :(得分:0)

你是怎么称呼你的表格的?

并删除 e.Cancel = True 将其更改为false 。 如果取消设置为true,则告诉VB.net取消关闭表单..

答案 2 :(得分:0)

这是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...