Close previous form

Posted on 2007-03-29
Last Modified: 2013-11-25
I need a quick fix piece of code that closes a window/form in a app.  when  the user clicks on the "save" button, the code passes parameters to the new form.  I  want the previous form to close at that point.

Thanks in advance.

Question by:TimSweet220
  • 3
  • 2
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18818661
We need  more info TimSweet220...

What version VB.Net?     2005?     2003 (or below)?

What is the "owner" of the previous form?  If the previous form was the "startup" form then closing it usually causes the app to close as well.

Are these standalone forms or Mdi Children?.


Author Comment

ID: 18819598
2003.  Separate forms.
LVL 85

Accepted Solution

Mike Tomlinson earned 500 total points
ID: 18819635
And is the "startup form" one of the forms that needs to be closed?

You can simply hide the current form and show the next one using ShowDialog().

If you want to actually close (dispose) of the current form then you have to go thru alot of shenanigans...

Do you need to go in both directions?...or only "forward"?
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.


Author Comment

ID: 18819730
no it's not a start up form.   I have a form  (form A) that the user fills out.  He clicks the "save" button and the data is saved then in the save function it makes a call the a second form.(Form B)  Form A stays open (behind Form B), I want to add something to the save function in Form that closes that form after it makes the call to Form B. Basically, I don't want the user to be able to go back to Form A with out opening it again from the menu.
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18819806
I think you've overcomplicated it...

In your main form, show FormA with ShowDialog().
When FormA closes, show FormB with ShowDialog().
You won't be able to interact with the main form until both forms are closed.

Something like...

Public Class YourMainForm

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim fa As New FormA
        Dim fb As New FormB
    End Sub

End Class

If you need to, you can create FormB and show it via ShowDialog() from the Closing() event of FormA:

Public Class FormA

    Private Sub FormA_Closing(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        Dim fb As New FormB
   End Sub

End Class

Form.ShowDialog() prevents interaction with anything else until that form is closed:

Expert Comment

ID: 18839598
Public Class FormA

    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click

        '-------Code to save data
        Dim FrmFormB As New FormB
        '-------If you want to pass any value to FormB
        '-------Store them on a public variable which is in FormB.
        '-------ex: FrmFormB.PassValue = 10

    End Sub
End Class

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
RUNRMTCMD from AS/400 12 68
VBA: copy range dynamically based on config sheet v2 3 48 threads keep increasing 2 35
DataGridView / get bound table name? 8 24
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

840 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