Connecting Multiple Forms

unknownloser

Newcomer
Joined
Feb 17, 2005
Messages
7
Location
USA
Hi. I have a program that begins with opening a form (frm1). I also have a button on the form that directs you to another form (frm2). WHen this button is clicked, frm1 is supposed to close, but it doesn't. Here is my code for the button:

Sub cmdClick

Dim form1 As New frm1
Dim form2 As New frm2

form1.Hide
form2.Show

End Sub

I'm used to working with VB5 And VB6, so if anyone could lend me a hand, I'd really appreciate it. Thanks.
 
Hi...

As I can see, the form that is keeping your app "alive" if Form1... so, if you just close it, you'll end the whole application...

So, what you need to do first is to change the form that is holding the messageloop, then show Form2, then close Form1...

Something like the following:

1st - Create a module, place the Sub Main on it like I show bellow.
Visual Basic:
Module Global

Public Form1 as Form1
Public Form2 as Form2
Public ctx As ApplicationContext

Public Sub main()
     Form1 = new Form1
     ctx = New ApplicationContext(Global.Form1)
     Application.Run(ctx)
End Sub

    Public Sub ChangeAppContext()
        Global.Form2 = new Form2
        Global.ctx.MainForm = Global.Form2
        Form1.Close()
        Form2.Show()
    End Sub

End Module

2nd - On the button, that shows the Form2, click event write:
Visual Basic:
     Global.ChangeAppContext

Thant's it...

Alex :p
 
Back
Top