Switching between two forms (VB2005)

JumpyNET

Centurion
Joined
Apr 4, 2005
Messages
196
I've always had to do this differently in every version of VB. So could someone please tell me what's the best practice in switching between two forms in the same application now in VB 2005?

I tried the following, but the program crashes if I close one of the forms by clicking the red cross on the form's top right corner and then try to switch again.
Visual Basic:
'Switching to form2 from form1
Form2.Show()
Form2.Select()

'Switching to form1 from form2
Form1.Show()
Form1.Select()

Could it be possible to cancel during unloading and hide the form instead?
 
Last edited:
Problem solved!!

OK, I found a way. :D I'll post the solution here incase I'm not the only one with this problem.

In form1:
Visual Basic:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Form2.Show()
        Form2.Select()
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        e.Cancel = True
        Me.Hide()
    End Sub

In form2:
Visual Basic:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Form1.Show()
        Form1.Select()
    End Sub

    Private Sub Form2_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        e.Cancel = True
        Me.Hide()
    End Sub
 
I usually figure out what form is the 'main' form, if you will and do the following on it.


Visual Basic:
Dim myForm as New Form2
myForm.Show()

A while back I was working on an IM client, and I used this methodology in addition to using a collection to keep track; worked very well.
 
Back
Top