???? Henry's VB.NET trip (11) - visual inheritance
???????????????????????? Han Rui?
The closed custom passed the inheritance of VB.NET in the morning, and the company was confident. White Manager saw me and asked: "How is learning? Is it possible to get the hand?" I nodded: "I should be doing something." "Ok," White Manager I took a shot of my shoulder and said, "I will have a chance to study with Da Li."
Get the leadership indication, I rushed into the cup of coffee, respectfully, I went to the big plum, put on his table. The big Li's mouth is a lot, smiled and said, "Don't be so polite, come, sit. Let me see how you inherit the study yesterday. Today, let's take a look at the new inheritance method provided by VB.NET, visualization inheritance."
"Visualization? Does it mean the inheritance mode that can be achieved in the visual editor?" I asked.
Big Li nodd: "Of course, it is to see the inheritance. Generally referring to the inheritance of the form."
I thought about saying: "In VB.NET, the form is already the form of classes. If you want to implement the form inherit, is it generated a base class form, then declare inherits in a new form in a new form Realize the inheritance of the base form "?"
"It is most like this, but today we come to see how to achieve this process by visualize compilation environments. First, it is built a base form." Big Li said, open the VB.NET compiler, from the menu Select to select "New" and "Project" from the File menu to open the New Project dialog. A Windows application named "Visual Inherit" is created.
Then right-click the "Visible Inheritance" project node in the Solution Explorer and select Properties. In the properties of the project, change the output type from the "Windows Application" to "Class Library", and then click OK.
Add a button to the form on the Windows Forms tab of Toolbox. Named BtnProtaced. Then in its Properties window, set the Text property to Protection, and set the Modifiers property to protected.
Then Big Li doubles the BTnProtected button, switch to the code window, write this code:
Private sub btnprotected_click (Byval Sender as system.object, _
? BYVAL E as system.eventargs) Handles btnprotaced.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 is set to Private.
Double-click the BTnPrivate button, 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, "Generate" from the Generation menu, generate a "visual inheritance .dll" file in the project's Bin folder.
"Is this possible to complete the construction of the base class form?"
"Yes," Da Li did not stop saying, "We will build a derived form through the integrated environment."
Big Li from the "File" menu "New Project" in "Add Project", created a Windows application called "Delivery Form". Right-click on the project node and select "Inherited Form" in "Add". In the pop-up "Add New Item" dialog box, you can see that "inherited form" has been selected, and Da Li hits "open". In addition, a "Inherited Selector" dialog box is pop-up, select "Form1" from "visual inheritance .dll" as the form you want to inherit from it, and then click OK.
So I created a form that Form1 derived from the "visual inheritance" project was named "inheritanceform".
Then, in the Windows Form Design, the inherit of the derived form comes with an arrow sign symbol.
Big Li dragged the "Protection" button and arbitrarily changed the size of the button. Then, turned the head to tell me: "You will try to change the size of the 'private' button." I don't think I have access to the mouse, but I will be able to click, drag and drop, it still doesn't move.
"Ah? What is wrong?" I was shocked.
"I have thought about it." Da Li began to taste my coffee.
I started to search for the two days of learning, slowly turned into the curved, "Private" button in the base form of the modifier is set to "private", which means it is derived. It is not possible to be changed. I carefully explained my thoughts to the big Li.
"Yes, don't be afraid." Da Li nodded, "The control attribute in the form will also decide whether the derived class can be changed. This button is set for Private, Its size and location properties are of course not changed in derived classes. "
Seeing that I thought about it, Da Li gave me a topic: "You will try to change the content of the message box when you click the 'Protect' button, change it to 'derived Protection button '. "
I followed Da Li in the workplace in the base form, double-click the button to switch to the code window. Write this code:
Private sub btnprotected_click (Byval Sender as system.object, _
Byval e as system.eventargs) Handles btnprotaced.click
??????? msgbox ("derived protection button")
End Sub
?
Next, I am right-clicking the "Delivery Form" item in Solution Explorer and selects "Set to Start Item". Then, right-click the "Derife" item in the Solution Explorer and select Properties. In the Derive Form Properties Page dialog box, set "Start Object" to inherited Form Inheritanceform.
Press the F5 key to run the app, and click the "Protection" button in Hairy.咦, the result will pop up two message boxes, the first pop-up is the button message "Protected button" written in the base class form, and then pops up the message "derived protection buttons". I was stunned, "I didn't rewrite the code of the Click event response program? Why didn't the message of the base class do not shield?"
Big Li Hao drinks a coffee and put down the cup. Take it to take me: "Shield? Can you block a member of the base class?"
"Ah?" I quickly saw the Click event handler in the base class form. "Really, it is private sub!" I smiled silly.
"For formal programming, the default is to handle itself. It does not take into account its inheritance, so the control's event handler is modified with private for packaging. But modification is also very easy, isn't it? ? "Da Li took me again and got up and went out.
I desperately smashed my face and began to recall the way to achieve inheritance, and finally started to change the program. First, write in the base class form:
Protected Overridable Sub btnprotected_click (Byval E AS System.EventArgs) Handles Btnprotaced.click
??????? msgbox ("protected control")
End Sub
?
I am preparing to rewrite the code to the derived class. Big Li is back. He stood behind me: "You don't worry about writing, look at the two drop boxes above the code window, the left is a class name, you choose The InheritanceForm class's Overrides, the name of the rewritable method appears in the right method name drop-down box. "
I will do it according to Da Li, really, in the method name drop-down box, I saw BtnProtected_Click, click on, there will be a blank code segment of the method, I add a message box to the code segment:
Protected Overrides Sub Btnprotected_Click (Byval Sender As Object, _
ByVal e as system.eventargs)
??????? msgbox ("derived protection button")
End Sub
?
run! Click the "Protection" button, and only one message box appears: "Detecting Protection Button".
(Endlessly)
-------------------------------------------------- -------------
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/latitude/