Solved

Close current form and open another one

Posted on 2004-04-20
11
286 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:nibirkhan
  • 4
  • 3
  • 2
11 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 10872876

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
 
LVL 8

Expert Comment

by:nishikanth
ID: 10874719
try this


Dim newStudent As New frmStudent()
newStudent.Show()
Me.close()
0
 
LVL 2

Author Comment

by:nibirkhan
ID: 10880524
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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 10881745
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
 
LVL 2

Author Comment

by:nibirkhan
ID: 10883340
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 10883357
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
 
LVL 2

Author Comment

by:nibirkhan
ID: 10883471
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
 
LVL 8

Expert Comment

by:nishikanth
ID: 10884692
then u must use the method Idle_Mind mentioned.... showdialog()
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 10885682
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

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

773 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