Solved

Close current form and open another one

Posted on 2004-04-20
11
284 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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 …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now