Close current form and open another one

hi,

I have two form, I want to close the current form and open another one with the same button click, like this:

Form1 : frmEmployee
Form2: frmStudent

When I click the "Close" button on frmEmployee, it will close it and will load frmStudent.

I tried me.close() and open another but it opens and get closed right away..

thanks
LVL 2
nibirkhanAsked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Alrighty....here is a different approach to the problem.

Below is code for a Form1 and a Form2.  An variable of type Form2 has been declared as WithEvents on Form1.  This allows to trap messages pertaining to our instance of Form2 from within Form1.

Whenever Form2 becomes visible, we hide Form1.  Whenever Form2 becomes not visible, either by closing it or hiding it, Form1 becomes visible again.

There is a button to exit the application from Form2.  Be aware that using Application.Exit() causes the application to exit without triggering any form closing events.

Regards,

Idle_Mind

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 showForm2 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.showForm2 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'showForm2
        '
        Me.showForm2.Location = New System.Drawing.Point(8, 8)
        Me.showForm2.Name = "showForm2"
        Me.showForm2.Size = New System.Drawing.Size(136, 24)
        Me.showForm2.TabIndex = 0
        Me.showForm2.Text = "Form2"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.showForm2)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private WithEvents f2 As Form2

    Private Sub showForm2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles showForm2.Click
        If f2 Is Nothing Then
            f2 = New Form2
        End If
        f2.Show()
    End Sub

    Private Sub f2_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles f2.Closed
        Me.Show()
    End Sub

    Private Sub f2_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles f2.VisibleChanged
        If f2.Visible Then
            Me.Hide()
        Else
            Me.Show()
        End If
    End Sub

End Class

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

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 showForm1 As System.Windows.Forms.Button
    Friend WithEvents exitApp As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.showForm1 = New System.Windows.Forms.Button
        Me.exitApp = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'showForm1
        '
        Me.showForm1.Location = New System.Drawing.Point(8, 8)
        Me.showForm1.Name = "showForm1"
        Me.showForm1.Size = New System.Drawing.Size(112, 24)
        Me.showForm1.TabIndex = 0
        Me.showForm1.Text = "Form1"
        '
        'exitApp
        '
        Me.exitApp.Location = New System.Drawing.Point(128, 8)
        Me.exitApp.Name = "exitApp"
        Me.exitApp.Size = New System.Drawing.Size(104, 24)
        Me.exitApp.TabIndex = 1
        Me.exitApp.Text = "Exit App"
        '
        'Form2
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.exitApp)
        Me.Controls.Add(Me.showForm1)
        Me.Name = "Form2"
        Me.Text = "Form2"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub showForm1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles showForm1.Click
        Me.Hide()
    End Sub

    Private Sub exitApp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitApp.Click
        Application.Exit()
    End Sub

End Class
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:

Dim newStudent As New frmStudent()
newStudent.Show()
Me.Dispose()

If frmEmployee is your startup form, then you would need to do it like this:

Dim newStudent As New frmStudent()
newStudent.ShowDialog(Me)
Me.Dispose()

To keep the whole application from closing when frmEmployee closes.
...or you have to create a Module with a Sub Main and load your forms from there.

Idle_Mind
0
 
nishikanthCommented:
try this


Dim newStudent As New frmStudent()
newStudent.Show()
Me.close()
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
nibirkhanAuthor Commented:
Idle Mind,

Thanks for the post, could you please explain me details, Yes my employee from is the start up form and I don't want to close the whole application when Emloyee from gets closed and student form opens since I have some more froms to open from the student forms and that time my student form will get closed and new form will be loaded, so for that 3rd form do I do the same thing?

thanks

nibirkhan
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Exactly....so you could put something like this in your button click event

Me.Hide()
Dim newForm As Form

' First form
newForm = New frmStudent()
newForm.ShowDialog(Me)
newForm.Dispose()

' Second form
newForm = New someOtherForm()
newForm.ShowDialog(Me)
newForm.Dispose()

' Third form
newForm = New yetAnotherForm()
newForm.ShowDialog(Me)
newForm.Dispose()

' all forms have been closed
' main form will now exit
' and the app will close
Me.Dispose()
0
 
nibirkhanAuthor Commented:
should I put the last line "Me.Dispose()" to each of my button? or what if I want to do this:

I want to open the Student form form the Employee form and Employee Form will be closed but I have a button on my student form that will close Student Form and again load the Employee Form?

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
If you want the Employee form to appear again after the Student form is close then do something like this in the button click event:

Me.Hide()
Dim newForm As Form
newForm = New frmStudent()
newForm.ShowDialog(Me)
newForm.Dispose()
Me.Show()

Idle_Mind
0
 
nibirkhanAuthor Commented:
thanks.. I tried that and it works well but the problem is I need to have two option from the Student Form, one is to go back to Employee Form and second one is to close the Student Form, if I close it with Me.close() , it closes but stay still on debud mode, I guess since Employee Form is hidden, I need to close the whole application form the student Form.

also here is a short question, how can I open a form as Popup from the student form?

thanks
0
 
nishikanthCommented:
then u must use the method Idle_Mind mentioned.... showdialog()
0
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.

All Courses

From novice to tech pro — start learning today.