Close previous form

Posted on 2007-03-29
Medium Priority
Last Modified: 2013-11-25
I need a quick fix piece of code that closes a window/form in a vb.net 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 86

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 86

Accepted Solution

Mike Tomlinson earned 1500 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 86

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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 process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

627 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