Open Another form and close itself

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....
joely2kAsked:
Who is Participating?
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
>> 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
 
Timbo87Commented:
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
 
joely2kAuthor Commented:
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
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
joely2kAuthor Commented:
Anyway I just learned vb.net last few days... sorry for a novice like me :)
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
joely2kAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
joely2kAuthor Commented:
THANKS you so much.... god bless you...
0
 
pogowolfCommented:
So what does it mean when the code:
  Dim frmA As formA = New formA

Errors with:
 Type 'formA' is not defined.

??

0
 
pogowolfCommented:
Nevermind..I'm a dork. ;)
0
 
5thcavCommented:
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
 
5thcavCommented:
Thanks,, If u like i'll open another question
0
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.

All Courses

From novice to tech pro — start learning today.