?
Solved

How do I close a Form from another Form?

Posted on 2005-03-03
5
Medium Priority
?
251 Views
Last Modified: 2010-04-23
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
Comment
Question by:bobbailey22
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 86

Expert Comment

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

Author Comment

by:bobbailey22
ID: 13454838
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13455174
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
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1500 total points
ID: 13455231
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
 
LVL 13

Expert Comment

by:crazyman
ID: 13457107
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month10 days, 20 hours left to enroll

770 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