Solved

Open Another form and close itself

Posted on 2004-10-27
52,927 Views
Last Modified: 2011-08-18
I know how to close and open a form...
Well.. but can you tell me how can I do this.....

Example...

There are 2 forms...named =>         formA  and   formB
and each of them have their own button named  => btnA   and    btnB

The application starts with formA

can you show me the code that... when I click on btnA in formA, It will CLOSE formA and open formB...

Then when I click btnB from formB, it will close formB and open back formA

How can I do this? I dun want their visible set to .false
I want them really close and use no resources from my computer.. How?
Thanks for your help....
0
Question by:joely2k
    13 Comments
     
    LVL 15

    Expert Comment

    by:Timbo87
    Form A to Form B:

    Dim FormB As New FormB()
    FormB.Show()
    Me.Close()

    Form B to Form A:

    Dim FormA As New FormA()
    FormA.Show()
    Me.Close()
    0
     
    LVL 85

    Accepted Solution

    by:
    >> The application starts with formA

    If you close formA, then the application will end.

    Here is an example of how to COMPLETELY close the current form and switch to another.  Add a module and paste the module code.  Add a class and add the class code.  Paste the code for forms formA and formB.  Be sure to set the project to start with Sub Main().

    ~IM

    ' ---------------------------------------------------------------------------------------------------------------------------

    Public Module MyApplication
        Public Enum MyForms
            formA = 0
            formB = 1
        End Enum

        Public Sub Main()
            Dim context As MyApplicationContext = New MyApplicationContext
            Application.Run(context)
        End Sub
    End Module

    ' ---------------------------------------------------------------------------------------------------------------------------

    Public Class MyApplicationContext
        Inherits ApplicationContext

        Public Sub New()
            MyBase.New()
            switchTo(Nothing, MyApplication.MyForms.formA)
        End Sub

        Private Sub switchTo(ByVal sender As Form, ByVal formName As MyForms)
            If Not (sender Is Nothing) Then
                sender.Dispose()
            End If
            Select Case formName
                Case MyApplication.MyForms.formA
                    Dim frmA As formA = New formA
                    AddHandler frmA.switchTo, AddressOf switchTo
                    AddHandler frmA.Closed, AddressOf OnFormClosed
                    frmA.Show()

                Case MyApplication.MyForms.formB
                    Dim frmB As formB = New formB
                    AddHandler frmB.switchTo, AddressOf switchTo
                    AddHandler frmB.Closed, AddressOf OnFormClosed
                    frmB.Show()

            End Select
        End Sub

        Private Sub OnFormClosed(ByVal sender As Object, ByVal e As EventArgs)
            ExitThread()
        End Sub
    End Class

    ' ---------------------------------------------------------------------------------------------------------------------------

    Public Class formA
        Inherits System.Windows.Forms.Form

        Public Event switchTo(ByVal sender As Form, ByVal formName As myForms)

        Private Sub btnA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnA.Click
            RaiseEvent switchTo(Me, MyApplication.myForms.formB)
        End Sub
    End Class

    ' ---------------------------------------------------------------------------------------------------------------------------

    Public Class formB
        Inherits System.Windows.Forms.Form

        Public Event switchTo(ByVal sender As Form, ByVal formName As MyForms)

        Private Sub btnB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnB.Click
            RaiseEvent switchTo(Me, MyApplication.MyForms.formA)
        End Sub
    End Class
    0
     

    Author Comment

    by:joely2k
    Dear Timbo87,
    you way wont work because when closed the formA the whole application will be closed

    Dear Idle_Mind
    Creditability to you, thanks you ...
    but besides this, is there any other easier / less complicated ways to do this? as I can managed more forms instead of switching 2 forms only.... for example formA to formB, formB to formC and more....

    but thanks anyway.. if you found some interesting tutorial or links do send to me...
    0
     

    Author Comment

    by:joely2k
    Anyway I just learned vb.net last few days... sorry for a novice like me :)
    0
     
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    The code can be extended to handle as many forms as you like.  Each form needs to have the switchTo() Event:

        Public Event switchTo(ByVal sender As Form, ByVal formName As myForms)

    Additionally, each form must be added to the myForms enum:

        Public Enum MyForms
            formA = 0
            formB = 1
        End Enum

    ...along with a corresponding Case section in the Select statement:

                Case MyApplication.MyForms.formA
                    Dim frmA As formA = New formA
                    AddHandler frmA.switchTo, AddressOf switchTo
                    AddHandler frmA.Closed, AddressOf OnFormClosed
                    frmA.Show()

    You can switch from one form to any other form by raising the event:

        RaiseEvent switchTo(Me, MyApplication.MyForms.formA)

    The first parameter is always Me [except for the first call in New() constructor of class MyApplicationContext], which causes the form raising the event to be closed.  The second parameter is the form to switch to and is one of the items listed in the myForms enum.

    That's it.  Study the code carefully and the pattern should become apparent.

    There are other approaches to the problem but I think this is the cleanest approach.  Your constraint that the forms be completely unloaded when switching really limits the possible solutions.

    ~IM
    0
     

    Author Comment

    by:joely2k
    May I know what is an Enum...

    so I can make it this way?

        Public Enum MyForms
            formA = 0
            formB = 1
            formC = 2
            formD = 3
           'and so on.......
        End Enum
    0
     
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Thats correct.  

    From the help file:
    An enumeration is a related set of constants. The enumeration members between the Enum and End Enum statements are initialized to constant values. The defined values cannot be modified at run time. Values can include both positive and negative numbers, as the following example shows:

         Enum SecurityLevel
            IllegalEntry = -1
            MinimumSecurity = 0
            MaximumSecurity = 1
        End Enum

    Enums are really helpful when you want a parameter or variable to be only from a certain number of predefined values.

    ~IM
    0
     

    Author Comment

    by:joely2k
    THANKS you so much.... god bless you...
    0
     

    Expert Comment

    by:pogowolf
    So what does it mean when the code:
      Dim frmA As formA = New formA

    Errors with:
     Type 'formA' is not defined.

    ??

    0
     

    Expert Comment

    by:pogowolf
    Nevermind..I'm a dork. ;)
    0
     
    LVL 7

    Expert Comment

    by:5thcav
    Idle_Mind

    what is the settings for this

    Public Event switchTo(ByVal sender As Form, ByVal formName As MyForms)
    gets
    MainForm.vb(34): 'formName' cannot expose a Friend type outside of the Public class 'MainForm'.
    this
    Public Event switchTo(ByVal sender As Form, ByVal formName As MyApplication.MyForms)
    gets
    MainForm.vb(34): 'formName' cannot expose a Friend type outside of the Public class 'MainForm'.

    i have tryed pointing to the forms, impoting you name it w/o success

    Below is just how i made it,, I dont thing this guy got it to work

    ####################################################################

    Module MyApplication
        Friend Enum MyForms

            MainForm = 0
            MainForm1 = 1

        End Enum

        Public Sub Main()
            Dim context As MyApplicationContext = New MyApplicationContext
            Application.Run(context)
        End Sub

    End Module

    Public Class MyApplicationContext
        Inherits ApplicationContext

        Public Sub New()
            MyBase.New()
            switchTo(Nothing, MyApplication.MyForms.MainForm)
        End Sub

        Private Sub switchTo(ByVal sender As Form, ByVal formName As MyForms)
            If Not (sender Is Nothing) Then
                sender.Dispose()
            End If
            Select Case formName
                Case MyApplication.MyForms.MainForm
                    Dim frmA As MainForm = New MainForm
                    AddHandler frmA.switchTo, AddressOf switchTo
                    AddHandler frmA.Closed, AddressOf OnFormClosed
                    frmA.Show()

                Case MyApplication.MyForms. MainForm1
                    Dim frmB As  MainForm1  = New  MainForm1
                    AddHandler frmB.switchTo, AddressOf switchTo
                    AddHandler frmB.Closed, AddressOf OnFormClosed
                    frmB.Show()

            End Select
        End Sub

        Private Sub OnFormClosed(ByVal sender As Object, ByVal e As EventArgs)
            ExitThread()
        End Sub

    End Class




    0
     
    LVL 7

    Expert Comment

    by:5thcav
    Thanks,, If u like i'll open another question
    0
     
    LVL 7

    Expert Comment

    by:5thcav
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: MongoDB Object-Document Mapper for NodeJS

    NodeJS (JavaScript on the server) is awesome, but some developers get confused about NoSQL when it comes to working in Node with MongoDB (NoSQL database). Do you need a better explanation of how to use Node.js with MongoDB? The most popular choice is the Mongoose library.

    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    This video Micro Tutorial is the second in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles a…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    934 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now