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
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
  • 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 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"?
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now


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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

705 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