In VB6, multiple forms can be easily invoked between each other, such as: In Form1, a form Form2 can be displayed with just one "form2.show" statement. However, the form processing mechanism has changed a lot in vb.net: Before accessing the form, you must instantiate the form, and if there are multiple code in the project that accesses the same form, you must pass the same instance pointer to the code, otherwise the newly created form instance is no longer the original form.
The following code implements the invocation of forms Form1 and Form2, Form1 as the primary form. The button on the Form1 BtnShowFrm2 is titled "Show Form2," and the Form2 button BtnShowFrm1 is titled "Show Form1."
1, the code in the Form1:
Public Class Form1
Inherits system.<a href= "http://dev.21tx.com/os/windows/" target= "_blank" >WINDOWS</A>. Forms.form
' Create a new instance of the Form2
Dim Frm2 as New Form2 ()
Public Function Instance2 (ByVal frm as Form2)
Frm2 = frm
End Function
Private Sub Btnshowfrm2_click (ByVal sender as System.Object, ByVal e as System.EventArgs) _
Handles Btnshowfrm2.click
' The following statement guarantees that when you access Form1 in Form2 and other forms,
' will get the same form instance of Form1.
Frm2.instance (Me)
Frm2.show ()
Me.hide ()
End Sub
End Class
2, the code in the Form2:
Public Class Form2
Inherits System.Windows.Forms.Form
Dim Frm1 as Form1
' Use a new instance property to generate an instance of the form Frm1
Public Function Instance (ByVal frm as Form1)
Frm1 = frm
End Function
Private Sub Btnshowfrm1_click (ByVal sender as System.Object, ByVal e as System.EventArgs) _
Handles Btnshowfrm1.click
Me.hide ()
Frm1. Show ()
End Sub
Private Sub form2_closed (ByVal sender as Object, ByVal e as System.EventArgs) Handles mybase.closed
' If Form2 is turned off, the button to set Form1 BtnShowFrm2 is not available.
Frm1. btnshowfrm2.enabled = False
Frm1. Show ()
End Sub
End Class