How to call each other between multiple forms

xiaoxiao2021-03-17  189

In VB6, multiple forms can be easily called each other, such as: In Form1, you only need to use a "FORM2.SHOW" statement to display form FORM2. However, in VB.NET, you have changed very much: Before accessing the form, you must instantiate the form; if you have multiple code to access the same form in the project, you must put it The same instance pointer is passed to these code, otherwise the newly created form instance is no longer the original form.

The following code implements mutual call between forms Form1 and Form2, and Form1 is the main form. The header of the button BTNShowFRM2 on Form1 is "Display Form2", and the title BTNSHOWFRM1 on Form2 is "displaying Form1".

1, the code in Form1:

Public Class Form1 Inherits System.Windows.Forms.Form 'Create a new instance of 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 ensures that the Form1 is accessed in Form2 and other forms,' will get the same form of form1. FRM2.INSTANCE (ME) frm2.show () me.hide () End Sub End Class

2, the code in Form2:

Public Class Form2 Inherits System.Windows.Forms.Form Dim frm1 As Form1 'means a property of generating a new Instance of the form frm1 example 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, set the FORM1 button BTNSHOWFRM2 is not available. FRM1.BTNSHOWFRM2.ENABLED = false frm1.show () end sub end class

转载请注明原文地址:https://www.9cbs.com/read-129722.html

New Post(0)