Question how to open a windows form from another

dev.sheoran

New member
Joined
May 10, 2010
Messages
2
Programming Experience
Beginner
I have two windows forms,say frm1 and frm2. I have to open frm2 from frm1 and frm1 from frm2.
when one form opens other hide. But the the problem is that everytime when i open the form new object
of the form is created which increase the memory usage of the application.
What should be the efficient way to switch from one window form to another in vb.net.

Thanks & Regards,
Dev
 

demausdauth

Well-known member
Joined
Mar 24, 2008
Messages
281
Location
Minnesota
Programming Experience
3-5
That depends a little on the purpose of the form that is being opened, what you will be doing with it.

The basic Show / Hide:
VB.NET:
'Form1
Private Sub btnOpenForm2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenForm2.Click
        Dim frm As frmTwo = Nothing

        If CommonCodeClass.IsFormAlreadyOpen(frmTwo) Then
            frm = DirectCast(My.Application.OpenForms("frmTwo"), frmTwo)
        Else
            frm = New frmTwo
        End If

        frm.Show()
        Me.Hide()

    End Sub


'Form2
Private Sub btnOpenForm1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenForm1.Click
        Dim frm As frmOne = Nothing

        If CommonCodeClass.IsFormAlreadyOpen(frmOne) Then
            frm = DirectCast(My.Application.OpenForms("frmOne"), frmOne)
        Else
            frm = New frmOne
        End If

        frm.Show()
        Me.Hide()

    End Sub

'CommonCodeClass
Public Shared Function IsFormAlreadyOpen(ByVal PassForm As System.Windows.Forms.Form) As Boolean

        Dim ReturnBool As Boolean = False

        For Each frm As System.Windows.Forms.Form In My.Application.OpenForms
            If frm.GetType() Is PassForm.GetType() Then
                ReturnBool = True
                Exit For
            End If
        Next

        Return ReturnBool

    End Function
 
Top Bottom