[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Open Another form and close itself

Posted on 2004-10-27
13
Medium Priority
?
53,047 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
Comment
Question by:joely2k
[X]
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
  • 4
  • 3
  • 3
  • +2
13 Comments
 
LVL 15

Expert Comment

by:Timbo87
ID: 12430129
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 86

Accepted Solution

by:
Mike Tomlinson earned 200 total points
ID: 12430310
>> 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
ID: 12433121
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

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

Expert Comment

by:Mike Tomlinson
ID: 12433317
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
ID: 12444952
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 86

Expert Comment

by:Mike Tomlinson
ID: 12448308
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
ID: 12453418
THANKS you so much.... god bless you...
0
 

Expert Comment

by:pogowolf
ID: 12475249
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
ID: 12475266
Nevermind..I'm a dork. ;)
0
 
LVL 7

Expert Comment

by:5thcav
ID: 14073917
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
ID: 14073936
Thanks,, If u like i'll open another question
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

656 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