开发者

Close all forms in vb

How can i close all forms


I use wpf vb 2010

To load other form i use like

Private frm As MainWindow = New MainWindow 

to open the window i used frm.Show() and to close it i use frm.Close()


Now I have two forms like开发者_开发百科 form1 and form2

I want to open form2 when i click on the button - ok it's easy

The Question How can i closed form1 from form2 when i open form2 using button

when i user

Private frm As form1 = New form1 
frm.Close()

It like a cricle and can't closed


The easiest way would be to create a constructor for Form2 that took a reference to an instance of Form1 when created. The new instance of Form2 could keep that reference and close the instance of Form1 when needed.

Public Class Form2

    Private Property formOne As Form1

    Public Sub New()
    End Sub

    Public Sub New(Form1 frm)
        formOne = frm
    End Sub

    Public Sub CloseFormOne()
        If formOne != null Then
            formOne.Close()
        End If
    End Sub

End Class


Project + Properties, Application tab, change Shutdown Mode to "When last form closes". This ensures that your app won't quit until all the windows are closed.

Now you can write code like this:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim frm As New Form2
    '' Optional: keep it in the same spot
    frm.StartPosition = FormStartPosition.Manual
    frm.Location = Me.Location
    frm.Size = Me.Size
    '' Display the form
    frm.Show()
    '' Close the current form
    Me.Close()
End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜