How multiple forms are invoked between each other

Source: Internet
Author: User

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



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.