?????????????????????????????????????????????????????????????????????????????????????????????????????????????????
??????????????????????????? Han Rui
I haven't had new work for a long time, I can't help with my friends, I have to take some old work to make up the shortcomings of recent writing. Ing! This series is different from the Henry's full-character, a role form, which was originally published in the Computer World. It is suitable for VB.NET beginners, and the old birds don't have to waste on my series of articles. Time. I hope that I have time to continue this series of articles, join a large number of engineering solutions. At the same time, my Henry is also written, and all friends who have supported my long-term support.
?
Background: I am Henry, I have just graduated from the university computer, join the Cabo company .NET development group; responsible for bringing my lead is a big plum, .NET master, is a humorous and patience guy; Lao White is our department The head often brings us the weird and hard demand.
On the first day, the White Manager took me to the .NET technology group reported. "Da Li, this is a new colleague, you have to help him." The old white pointed to me to a young man in a glasses.
"No problem, he will do it very well." Don't Dafu will see it? " Or is it a welcome? No matter, I just have to work hard.
After 20 minutes, I have already assigned a P4 computer, and the integrated compilation environment (IDE) of Visual Studio.net is started under the WindowsXP platform. A more beautiful compilation environment, the style when you can use it is VB6 or VC6. I chose the VB6 style with good RAD performance.
"You just started to contact .NET, deepen the impression before system training, see what VB.NET is different from VB6. You create two forms, each has a button on each form, hide it when you click The form of the form is switched out of another form. "Da Li's voice sounded in the ear, interrupted my appreciation of IDE. But what he mentioned is also too simple. Only two sentences in VB6 need to write the statement in the button in the response:
Two forms: Form1 and Form2. The buttons on the form1 are: command1, the buttons on the form2 are: Command2
Private submmand1_click ()
? Me.hide
? Form2.show
End Sub
Private sub fascist2_click ()
? Me.hide
? Form1.show
End Sub
So I didn't hesitate to start my first VB.NET project in vs.net. First, a Windows application project for VB.NET is created automatically generated for Windows Form: Form1, add a button control: Button1; then, add a Windows form: form2, add a button on it. Control: Button2.
After the design window double-click Button1, the compiler switches to the click response code of Button1 of the code window.
After the code segment is hurt, when I want to write form2.show, I have a point after Form2, but there is no Show method in the intelligent perceived menu. What is it like this? Is Vb.Net modified so big. I looked up and looked at the big Li standing next to me.
Big Li immediately smiled, it seems to be in the middle of his circle. His finger point to the screen, point the beginning of the code in Form1:
Public Class Form1
I'm not stupid. I immediately understood that now Form1 is a class that inherits from system.windows.forms.form. When the call is called, it is necessary to instantiate first. It is not difficult. Have started:
Private sub button1_click (byval sender as system.Object, _
? BYVAL E as system.eventargs) Handles Button1.click
Me.hide ()
??????? DIM FRM2 AS New Form2 () 'instantiation FORM2 class to get an object
FRM2.SHOW ()
End Sub
A piece of code is also likely to write a code in Form2's Button2, running, and the effect of VB6 is almost. In proud, Da Li came over, adding a text box TextBox1 in my Form1, emptying the text. Then run the program. In the run, he wrote a few words in the text box: "Henry's work", then click Button1 to switch to FORM2, then click Button2, reappear, it turns into blank.
"This ..." I will slowly think about something, "should be the program through the new construction method, each time the button will re-instantiate the FORM class? So each re-appearing form is not previous The one that appeared. The previous form is missing, can't pass it. "
"Disappearance?" Da Li couldn't help but laugh, "Yes, missing in memory. Every time you instantiate, the system will open a memory to an object, and there will be corresponding threads to control and manage it. If As the first FORM1 of the main thread in the process is gone by hide, it does not turn off it, even if you turn off all the forms, the process will not be suspended. So, what method uses to find your missing form , Or how only one object is used, no longer instantified each time? "
"Global object!" I suddenly out, I feel that Da Li is not in order, but I have to continue to say: "Use global variables to make Form1 and Form2 only instantiate once, so it is possible."
Then, under the gaze of Da Li, I started to implement my plan:
Create a module file: Module1.vb, and at the same time:
Solution Manager -> Project Name. Sln -> Right Mouse -> Properties -> Universal Properties -> General -> Start Object -> Change to Module1. Let the program start running the code instructions in Module1 before writing the code as follows:
'Module1.vb
Module Module1
??? PUBLIC FRM1 AS NEW FORM1 ()? 'Defines the common variable instance of Form1
??? public fm2 as new form2 () 'Defines the common variable instance of Form1
??? Sub main ()
??????? Application.run (frm1) 'This sentence indicates that the program will then run the FRM1, which is an instance of Form1. The main thread becomes FRM1 in the runtime.
??? End Sub
End module
In this way, FRM1, FRM2 becomes a global variable, and only written in Button1 and Button2, as long as FRM1.SHOW is OK.
Run the program again, Yenry's works will always remain in the text box in Form1.
Dafu didn't wait for me to laugh again, when FRM1 hidden, close the Form2 window (FRM2 instance), there is no form on the desktop, and the process is still in the case where it cannot be suspended. My god, object-oriented VB.NET, love you is so difficult ... ------------------------------- --------------------------------
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
E-mail: Henry7685@hotmail.com
Please visit my column:
http://www.9cbs.net/develop/author/netauthor/ilatitude/