?
Solved

Moving Between Forms without creating new instances

Posted on 2004-11-30
8
Medium Priority
?
245 Views
Last Modified: 2012-05-05
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?
0
Comment
Question by:Venom1234
8 Comments
 
LVL 3

Expert Comment

by:jazduck
ID: 12713222
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
 
LVL 3

Expert Comment

by:jazduck
ID: 12713232
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
 

Author Comment

by:Venom1234
ID: 12713236
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Venom1234
ID: 12713256
Hey Jaz,
Even with the change it requires me to pass it the variables
Thanks for the quick replies!
0
 
LVL 3

Expert Comment

by:jazduck
ID: 12713309
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
 
LVL 8

Accepted Solution

by:
rfgkev earned 1000 total points
ID: 12714136
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12716608
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
 

Author Comment

by:Venom1234
ID: 12725546
Thanks everyone I got it done with a little help from rfgkev
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question