Henry Instructions - A Probe into FORM in VB.NET WinForm (2)
Han Rui (2002.9.25)
Second, how to switch two forms?
In the previous section, we realize the new features of the VB.NET form, and know how Show has a form. Then face a such need (also a problem with more real). How to achieve switch between two windows. which is:
Form 1: Form1, there is button1
Form 1: form2, there is a Button2
Click on Button1, will show Form2; click on Button2, will show Form1.
If in VB6, we really don't have to write articles to discuss // HEHE. In the previous section, we discuss the key difference between Visual Basic .NET and Visual Basic6 in the form processing mechanism - you only create a form example before you can display the form appearance, access the form properties, and its controls.
The discussion in this section is another difference: Visual Basic 6.0 project automatically created the default form examples can be used as global variables, that is, any code in the project can directly reference the form, and each time it is referenced All are the same example of the form. VB.NET? Can you do this.
Let's take a small experiment to deepen the concept of class with objects.
You put four buttons in a form frmmain, write on the Click event of each button:
DIM FRM1 AS New Form1
FRM1.SHOW
So you will see four exact same FORM1.
you got it? However, this FORM1 is not a modified content of the Form1 class.
So we can't expect to name a local FRM1 to control the instance of Form1, it is impossible.
That is, write in the Click event of Button1:
DIM FRM2 AS New Form2
FRM2.SHOW
Me.hide
Write in the Click event of Button2:
DIM FRM1 AS New Form1
FRM1.SHOW
Me.hide
Is there a problem, have you found the problem? Just in the Click event is partial variables, each time a new form is generated, and the example of the form that hide is again SHOW is not back. If you do not do anything in Form's Closing or other similar events, the process will not stop, this is because the first FORM1 for the main thread is from HIDE, it is still Run, the process is of course not terminated.
There are many solutions, such as
1) Define FORM global variables in Module
2) Implementation of form switching in VB6 with Share variable
One explains, I hope to inspire the friends:
1) Define FORM global variables in Module
Add a module to the program engineering to store public variables, and start control:
'Module1.vb
Module Module1
Public F1 AS New Form1 () 'Defines the common variable instance of Form1
Public F2 AS New Form2 () 'Defines the common variable example of Form1
Sub main ()
Application.run (f1) 'This sentence indicates that after the program starts Modele, it will be next to start F1, which is an instance of Form1.
End Sub
End module
'FORM1.VB
Class Form1
...
PRIVATE SUTTON1_CLICK (Byval e as system.Object, byval e as system.eventargs) Handles button1.clickf2.show () 'Using public variables
F1.hide ()
End Sub
END CLASS
'Form2.vb
Class Form2
...
Private sub button1_click (byvale as system.object, byval e as system.eventargs) Handles Button1.click
F1.show ()
F2.hide ()
End Sub
END CLASS
Also in: Solution Manager -> WindowsApplication1.SLN -> Right Mouse -> Properties -> Universal Properties -> General -> Start Object -> Change to Module1
The last sentence above means that the program will execute the code in Module1 during startup. Initialize two forms of common variables, the process inlet is in its SUB Main, used to start F1 (instance of Form1). With this method, we can complete a lot of formal control controls (such as operation results on Form1) and Data delivery in the text box on Form2. It should be noted here that the program will only be terminated only if the F1 form is turned off. We can do some processing, such as adding end, etc. in Form's Closing event.
However, in VB.NET is actually disapproving from the use of public variables, one is due to the management of the survival period, the second is the chaos when the reference is quoted. So, there is any other solution?
2) Implementation of form switching in VB6 with Share variable
In fact, there is a simple but very useful method, don't you know if you think? That is to upgrade the upgrade wizard tool with VB.NET! (Although many people can't do this tool, some work can still be done well)
I did this, I wrote two forms in VB6.
Form1:
Private submmand1_click ()
FORM2.SHOW
Me.hide
End Sub
Private Sub Form_Unload (Cancel AS Integer)
End 'Close Form1, the end of the program
End Sub
FOMR2:
Private sub fascist2_click ()
FORM1.SHOW
Me.hide
End Sub
Private Sub Form_Unload (Cancel AS Integer)
Form1.show Turn off FORM2 to call FORM1
End Sub
After the upgrade, there are more "upgrade support" that is folded with Region.
Form1:
#Region "Upgrade Support"
Private shared m_vb6formdefinstance as form1
Private shared m_initializingdefinstance as boolean
Public Shared Property Definstance () AS Form1
Get
IF m_vb6formdefinstance is nothing orelse m_vb6formdefinstance.isdisposed the
m_initializingdefinstance = true
m_vb6formdefinstance = new form1 ()
m_initializingdefinstance = false
END IF
Definstance = m_vb6formdefinstancend get
Set
m_vb6formdefinstance = value
End set
End Property
#End region
Private submmand1_click (Byval Eventsender As System.Object, Byval Eventargs As System.EventArgs) Handles Command1.click
Form2.definstance.show ()
Me.hide ()
End Sub
Private sub form1_closed (byval eventsender as system.object, byval eventargs as system.eventargs) Handles mybase.closed
End
End Sub
In Form2, the code except the upgrade support is:
Private submmand2_click (byval eventsender as system.object, byval eventargs as system.eventargs) Handles Command1.click
Form1.definstance.show ()
Me.hide ()
End Sub
Private sub form2_closed (byval eventsender as system.object, byval eventargs as system.eventargs) handles mybase.closed
Form1.definstance.show ()
End Sub
In the above code, it is important to note that using form1.definstance.show () to show an instance of Form1. Definstance is the property of Shared, which can be directly accessed in the form of "form name .definstance". Any code in its project access it will get the same form instance.
In this way, we can simulate the direct reference to the form of the Form of the VB6, but only replace Form1.definstance in the code. If you want to set up a Label on the form, it is now used: form1.definstance.label1.text = "Henry"
We can completely click "Upgrade Support", then pay attention to the content I wrote in two paragraphs, you can write it yourself without upgrade tools.
----
Disclaimer: The right to copyright and interpretation of this article belongs to Han Rui, if you need to reprint, please keep your full content and this statement.
QQ: 18349592