About the Visual Basic 2005 form of exchange of the technical thinking
Source: Internet
Author: User
Every day in visual, VB6 programmers begin to move into. NET development, and the most important problem they face is to get used to the golden rule of the object-oriented. Net. As a result, the forum always hears netizens talk about how to access the forms in Visual Basic. NET 2003. Earlier, I specifically published an article on this problem solution that uses the Me keyword to get an instance of another form in a new form by rewriting Sub New. Now that the era of VB2005 is coming soon, will there be a simpler way? The answer is yes, all mysteries are hidden in the My.Forms namespace!
In fact, there is nothing to note here, the maintenance of any form class default instance, in the early version of VB provides this feature. In the same way that the. NET 2003,VB to pursue and other object-oriented language styles, he reluctantly cut off this feature. At the same time as VB2005, Microsoft seems to be looking more rationally at the features that have been removed from the VB6 (existence is justified, isn't it?). , maintaining the default class of forms is a technique that is recovered. Just now, all the default instances of all forms are unified under the My.Forms namespace (for example, My.Forms.Form2), changing the style of chaos in the VB6 (Oops, the Nose of Warcraft play has gone mad, hehe). However, you can still omit the preceding my.forms when writing code, such as you can write in Form2.vb:
Private Sub button1_click (ByVal sender as System.Object, ByVal e as System.EventArgs) _
Handles Button1.Click
Form1.text = "Hello my.forms"
End Sub
Haha, did you find the feeling of VB6 again?
In fact, I have just come into contact with the characteristics of 2005, I still quite a bit not adapt to it. Because I'm used to developing under 2003, I always go to the new form myself. So I had a whim to think, if every start program, is not my.forms for us to automatically generate a default form instance, if we do not use this instance, it is very wasteful memory? In fact, this is not the case, I think it is necessary to say the following here.
When Visual basic executes your code, when it encounters a reference to an instance under My.Forms, Say,form2, the first thing it does is to determine whether the default instance of the Form2 form (note that the default instance is not your own new instance) has been created. If so, refer to the instance, or, if not, create a default instance. In other words, if MY.FORMS.FORM2 is not present in your code, the default instance of Form2 will not be created at all! So, the My.Forms namespace simply maintains the default instance of the form automatically, rather than automatically creating it. Similarly, My.Forms is generally not responsible for automatic collection of default instances.
I've been stressing the word "default" above, because I want you to remember that by default, after all, it's a little bit of a limitation: if you have to make a form instance yourself, it won't be included in the My.Forms space. So I would like to say that if you have a form in your program that needs to be created in many instances, it is still necessary to master the form exchange techniques in VB2003. My previous article was a bit rushed, and I didn't write down the details of the code, so some netizens complained to me. Take advantage of this opportunity to explain to me in detail:
I still assume that your main form class is named Form1, and the new form class is named Form2, so you can now rewrite the Form2 Sub New so that it accepts instances of the main form. I'm afraid you've found out you can't find the original sub New, have you? Yes, VB Beta has hidden the code that Windows automatically generates forms to programmers. Did you think of VB6? Oh, you and I have the same illusion, hehe. In fact, the code is not hidden, but was separated into the Form2.Designer.vb file under the (Do not believe, you go to open to see). In fact, this is a new amazing feature in VB2005, called partial types or partial type. Limited to space, about some types of introduction, here Conlio.
First, define a global variable of the Form1 type in Form2.vb:
Dim Frmmain as Form1
Then rewrite sub New so that frmmain points to the incoming Form1 instance:
Sub New (ByVal frm as Form1)
MyBase.New ()
InitializeComponent ()
Frmmain = frm
End Sub
Back to Form1.vb, we created an instance of Form2 like this:
Dim frmnew as New Form2 (Me)
Now you can call the main form instance in Form2.vb.
When it comes to sub New, you can also rewrite it directly in Form2.Designer.vb, but since Microsoft has separated the code to avoid contamination, I wouldn't recommend it unless you're a veteran.
At the end of this article, let's go back to the My keyword for visual Basic 2005. My show to us of her vibrant side, but you look carefully, incredibly can find some of the taste of innocence, such as forms of exchange, is so. In short, my is a new feature worth noting in VB2005. If you want to know more about my keyword now, you can first refer to my simple translation of my, and then try it myself under IntelliSense. If you want to learn more about my, including some of the types mentioned above, go to the head of my senior Ninputer's blog assembly (which is the most detailed one I have seen so far). I think, every new feature you know about me, you will make a sigh: "Oh, My god!"
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.