Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

How do I close a Form from another Form?

I have a LinkLabel on Form1 and when it's clicked I want Form2 to open and Form1 to close.


Dim frmForm2 as New frmForm1
frmForm2.ShowDialog()
Me.close()

The code will open form 2 but will not close form 1 until form 2 is closed. Form 2 must open with .ShowDialog() not .Show()

How can I get form 1 to close when form 2 opens?
0
bobbailey22
Asked:
bobbailey22
  • 3
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
If your using ShowDialog() then the code in Form1 is halted until Form2 is closed.  You can just do this then:

    Dim frmForm2 as New frmForm1
    Me.Hide()
    frmForm2.ShowDialog()
    Me.Show()    
0
 
bobbailey22Author Commented:
Me.Hide() will not work as the form1 has DataLoaded from a database and loading Form2 with Form1 open (but hidden) will cause concurencey issues. I did try using Me.Dispose() which does work but will it casue memory leaks? What is the difference between Me.Dispose and Me.Close?

Thanks for your time, Bob
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
The Close() method causes the Closing() event to fire and thus any cleanup code to run.  The Dispose() method just kills the form and doesn't generate a Closing() event as demonstrated in this example:

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close() ' You will get the "Closing" message box below
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Dispose() ' You will NOT get the "Closing" message box below
    End Sub

    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        MsgBox("Closing...")
    End Sub

End Class
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Also, take a look at the solution I posted in this PAQ.  It shows how to switch between forms in application in a way that allows the previous form to be completely closed without the application quitting:
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21185658.html
0
 
crazymanCommented:
In form1
 
Private  Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
               Dim f As Form2 =  New Form2()
               f.Owner = Me
               Me.Hide()
               f.ShowDialog()
End Sub
 
 
in Form2
 
Private  Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
               Me.Hide()
               (CType(Me.Owner, Form1)).ShowDialog()
End Sub
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now