Moving Between Forms without creating new instances

I am working on a VB.Net program where I am passing variables between forms and also reusing forms.  What I want to be able to do is when you got to a form (Call it Form B)  the one you were at before (Form A) disapears but when you finished with FORM B you can go back to the hidden form (Form A) exactly where you left off without having to pass it new variables.   I've experimented with .Hide and .Visible = False but the problem is I can't get it to come back (.Show or .Visible=True want variables to instanciate it.)  Any Ideas?
Venom1234Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jazduckCommented:
Form1 Code:

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(80, 56)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(104, 24)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Button1"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1})
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region
    Dim frm2 As New Form2()

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Hide()
        frm2.Show()

    End Sub
End Class

Form2 Code:
Public Class Form2
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(40, 48)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(144, 32)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Button1"
        '
        'Form2
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1})
        Me.Name = "Form2"
        Me.Text = "Form2"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Frm1 As New Form1()
        Me.Hide()
        Frm1.Show()
    End Sub
End Class
0
jazduckCommented:
On Form2 code to avoid a new instance just move the Dim Frm1 as New Form1() line to outside of the button event, as with form1.. just a minor mistake :)
0
Venom1234Author Commented:
Thanks JazDuck but this creates a new instance of the form I can't do that.. I need to be able to take a form and just hide it  and get it back exactly the way it was before without having to pass it variables again...  
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Venom1234Author Commented:
Hey Jaz,
Even with the change it requires me to pass it the variables
Thanks for the quick replies!
0
jazduckCommented:
Hmm, ill have more of a look, I didnt think that Hide disposed of the control data, the other option if it wont preserve the data in the controls is to output the relevant data to a place of your choice and read it on the forms load or activate.  Having a look for a 'nicer' way of doing it tho.
0
rfgkevCommented:
In form A

    Private Sub Button_Click(blah)
        Dim oForm as new FormB
        me.Visible = False
        oForm.ShowDialog
        me.Visible = True
    End Sub


Alternatively, you could add a member variable to each form with a property for it as such

Public Class FormB
      blah

    Private moParent As System.Windows.Forms.Form

    Public Property MyParent() As System.Windows.Forms.Form
        Get
            return moParent
        End Get
        Set(Value As System.Windows.Forms.Form)
            moParent = Value
        End Set
    End Property


Then when you create your form in form a, set is property like this

    Private Sub Button_Click(blah)
        Dim oForm as new FormB
        oForm.MyParent = Me
        oForm.Show
    End Sub

This sets it to reference the current form, then in your formB you can access it via the member variable moParent so you can go moParent.Show, moParent.Hide etc...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike TomlinsonMiddle School Assistant TeacherCommented:
Hi Venom1234,

The following is a modification of the accepted solution I posted in this PAQ:
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21185658.html

In your case, you want the forms to hide instead of close.  Add a module and paste the module code.  Add a class and add the class code.  Be sure to set the project to start with Sub Main().

The example below has only two forms, Form1 and Form2, but the solution can be extended to handle any number of forms.  Just remember to add each of your form to the Enum, MyApplicationContext class and the Case statement.  Each form must expose the switchTo() event as shown below.

Hope it helps,

Idle_Mind

' ---------------------------------------------------------------------------------------------------------------------------

Public Module MyApplication

    ' add all of your forms to the enum below
    Public Enum MyForms
        Form1 = 1
        Form2 = 2
    End Enum

    Public Sub Main()
        Dim context As MyApplicationContext = New MyApplicationContext
        Application.Run(context)
    End Sub

End Module

' ---------------------------------------------------------------------------------------------------------------------------

Public Class MyApplicationContext
    Inherits ApplicationContext

    ' add all of your forms here...
    Private frm1 As Form1
    Private frm2 As Form2

    Public Sub New()
        MyBase.New()
        ' formA will be the starting form
        switchTo(Nothing, MyApplication.MyForms.Form1)
    End Sub

    Private Sub switchTo(ByVal sender As Form, ByVal formName As MyForms)
        If Not (sender Is Nothing) Then
            ' hide whomever we just came from
            sender.Hide()
        End If

        ' switch to the requested from...creating a new instance ONLY IF necessary
        Select Case formName
            Case MyApplication.MyForms.Form1
                If frm1 Is Nothing Then
                    frm1 = New Form1
                End If
                AddHandler frm1.switchTo, AddressOf switchTo
                AddHandler frm1.Closed, AddressOf OnFormClosed
                frm1.Show()

            Case MyApplication.MyForms.Form2
                If frm2 Is Nothing Then
                    frm2 = New Form2
                End If
                AddHandler frm2.switchTo, AddressOf switchTo
                AddHandler frm2.Closed, AddressOf OnFormClosed
                frm2.Show()


                ' add your other forms as seperate case statements here...

        End Select
    End Sub

    Private Sub OnFormClosed(ByVal sender As Object, ByVal e As EventArgs)
        ' if any of the forms are closed...then the application will close
        ExitThread()
    End Sub

End Class

' ---------------------------------------------------------------------------------------------------------------------------

Public Class Form1
    Inherits System.Windows.Forms.Form

    ' all of your forms must have the event below
    Public Event switchTo(ByVal sender As Form, ByVal formName As MyForms)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' switch to whatever form is in the second parameter
        RaiseEvent switchTo(Me, MyApplication.MyForms.Form2)
    End Sub

End Class

' ---------------------------------------------------------------------------------------------------------------------------

Public Class Form2
    Inherits System.Windows.Forms.Form

    ' all of your forms must have the event below
    Public Event switchTo(ByVal sender As Form, ByVal formName As MyForms)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' switch to whatever form is in the second parameter
        RaiseEvent switchTo(Me, MyApplication.MyForms.Form1)
    End Sub

End Class

' ---------------------------------------------------------------------------------------------------------------------------
0
Venom1234Author Commented:
Thanks everyone I got it done with a little help from rfgkev
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.