Link to home
Start Free TrialLog in
Avatar of joely2k
joely2k

asked on

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....
Avatar of Timbo87
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()
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of joely2k

ASKER

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...
Avatar of joely2k

ASKER

Anyway I just learned vb.net last few days... sorry for a novice like me :)
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
Avatar of joely2k

ASKER

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
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
Avatar of joely2k

ASKER

THANKS you so much.... god bless you...
So what does it mean when the code:
  Dim frmA As formA = New formA

Errors with:
 Type 'formA' is not defined.

??

Nevermind..I'm a dork. ;)
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




Thanks,, If u like i'll open another question