Henry's Journey to the VB.net (11)-Visual inheritance

Source: Internet
Author: User
Tags implement inheritance modifiers
Henry's vb.net Journey (11)-Visual inheritance

Han Rui

I went to the retreat to practice a night of VB.net's inheritance program, and I was confident when I entered the company in the morning. The white manager saw me and asked, "How's the study going?" Are you ready for vb.net? "I nodded:" should be able to do something. "Well," white manager patted me on the shoulder and said, "with big Lee to study for a few days, there will be a chance." ”

Having been instructed by the leader, I made a cup of coffee and respectfully went to Big Lee and put it on his desk. Big Li Mouth a Qiao, smiled and said: "Don't be so polite, come, sit." Let me see how you inherited your study yesterday. Today, let's take a look at the new inheritance method provided by vb.net, visual inheritance. ”

"Visualization?" Does that mean the inheritance that can be implemented in a visual editor? I followed the question.

Big Lee nodded and said, "Of course, it's a visible inheritance." Generally refers to the inheritance of the form. ”

I thought for a moment and said: "In vb.net, the form is already the form of a class, if you want to implement form inheritance, do you generate a base class form and then implement inheritance of the base class form in the new form by declaring inherits?" ”

"This is generally true, but let's take a look at how you can do this through a visual compilation environment." First, you build a base class form. Big Lee said, while opening the vb.net compiler, select from the menu from the File menu, select New and Project, and open the New Project dialog box. A Windows application named "Visual Inheritance" was created.

Then right-click the Visual Inheritance project node in Solution Explorer and select Properties. In the project's properties, change the output type from Windows application to class library, and then click OK.

Add a button to the form on the Windows Forms tab of the Toolbox. Named Btnprotected. Then, in its Properties window, set the Text property to protected and set the modifiers property to Protected.

Then the big Lishuang hits the btnprotected button, switches to the Code window, and writes this code:

Private Sub Btnprotected_click (ByVal sender as System.Object, _

ByVal e as System.EventArgs) Handles Btnprotected.click

MsgBox ("protected button")

End Sub




Then switch back to the design window, add the second button btnprivate, and set the Text property to private, and the modifiers property set to personal.

Double-click the Btnprivate button to add the following code:

Private Sub Btnprivate_click (ByVal sender as System.Object, _

ByVal e as System.EventArgs) Handles Btnprivate.click

MsgBox ("Private button")

End Sub




Finally, from the Build menu, choose Build to generate a visual inheritance. dll file in the project's Bin folder.

"Is this the way to complete the construction of the base class form?" ”

"Yes," he said. The big Li didn't stop and said, "Let's build a derivative form by integrating the environment." ”

Big Lee. From the File menu, select New project in Add Project to create a Windows application named "derived form." Right-click the project node and select Inherited form in Add.

In the Add New Item dialog box that pops up, you can see that the inherited form has been selected, and big Lee clicks Open. Also pops up an inheritance Selector dialog box, select "Form1" from "Visual inheritance. dll" as the form from which to inherit, and then click OK.

A form derived from the form Form1 in the Visual Inheritance project is created in the derived form project, named "Inheritanceform."

In the Windows Forms Designer, the derived form inherits the button in the upper-left corner with an arrow glyph.

Big Lee dragged a "protection" button and arbitrarily changed the size of the button. Then, turn around and say to me, "you try to change the size of the ' private ' button." "I disagree with the mouse, but, let me how to click, drag, it is still feuds."

"Ah?" he said. What's the problem? "I was surprised.

"Think about it yourself." "Big Lee began to taste the coffee I poured to him.

I began to search the brain for these two days of learning, slowly turning the corner, the "private" button in the base class window modifiers are large Li She "private", meaning that it can not be changed in the derived class. I carefully explained my thoughts to big Li.

"Yes, don't be afraid to say it wrong. "Big Lee nodded," the control properties in the form also follow the modifier settings to determine whether the derived class can change it. This button is set to private, and its size and position properties are certainly not changed in the derived class. ”

See my head thoughtfully, Big Lee gave me a question: "You try to change a little bit. In a derived form, when you click the ' Protect ' button, the content of the pop-up message box is changed to ' derived protection button '. ”

I'm with Big Lee. As in the base class form, double-click the button to switch to the Code window. Wrote this code:

Private Sub Btnprotected_click (ByVal sender as System.Object, _

ByVal e as System.EventArgs) Handles Btnprotected.click

MsgBox ("Derived protection button")

End Sub




Next, I right-click the derived form project in Solution Explorer and select Set as Startup Project. Then, right-click the derived form project in Solution Explorer and select Properties. In the Derived Form Property Page dialog box, set the Startup object to the inherited form Inheritanceform.

Press F5 to run the application and click the "Protect" button with delight. Gee, the result pops up two message boxes, the first one pops up is the button message content "protected button" in the base class form, and then pops up the newly-written message "Derived protection button". I was stunned, "I have not already rewritten the Click event Response program code?" Why is the base class message not blocked? ”

Big Lee Youran Drink a cup of coffee, put down the glass. Patted me and said: "Shielding?" Can you screen the private members of the base class? ”

"Ah?" he said. I hastened to look at the Click event handler in the base class form. "Really, it's private sub!. I smiled innocently.

"For form programming, the default is to handle itself, without taking into account its inheritance, so the control's event handlers are decorated with private, for encapsulation." But it's easy to change, isn't it? Big Lee patted me again, got up and went out.

I rubbed my face desperately, began to recall the way to achieve inheritance, and finally began to change the program.

First, write in the base class form:

Protected Overridable Sub Btnprotected_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Btnprotec Ted. Click

MsgBox ("Protected controls")

End Sub




I am ready to rewrite the code in the derived class, Big Lee came back, he stood behind me and said: "You do not rush to write, look at the code window above the two Drop-down box, the left one is the class name, you choose Inheritanceform class overrides, The name of the method that can be overridden appears in the Drop-down box on the right method name. ”

I do according to Big Lee, sure enough, in the Method name dropdown box to see the Btnprotected_click, click on it, there will be a blank code section of the method, I added to the code snippet a message box content:

Protected Overrides Sub Btnprotected_click (ByVal sender as Object, _

ByVal e as System.EventArgs)

MsgBox ("Derived protection button")

End Sub




Run! By clicking on the "Protect" button, there is only one message box: "Derived protection button."

(To be continued)



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.