|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
Table of contents
What is the need of Forms Inheritance?So, the biggest question before reading this article - what is the need of Forms inheritance. First, we will be coding all the base/common code into the Base form (any Form from which we want to inherit). Then instead of copying or writing some global functions to change the properties or call methods, etc. to use some functionality of the base form (as we use to do in our earlier Languages), we can use form inheritance here. We can inherit the base form into the form where we want to implement the common functionality. As we inherit the form all the public/protected functionality of the base form gets merged into child form. For example: In a common scene, we have to design navigationa panel in our application to navigate from one record to another. Previously to avoid the repeatable code we use ActiveX Control. First we deisgn the activeX Control for the navigation and then we place this onto our form where we wanted to implement the functionality. But this time, we can code the functionality of the navigation panel in the base form and then inherit this form to achieve a similar in the child and make the best use of Form inheritance. Most of the times, when we want to give the functionality of navigational panels, status bars, info labels, etc in your project you can use form inheritance there. Code all these in the Base form and then inherit the base form into the forms where you want to implement the same functionality. If you want you cal also override some/all functionality of the Base forms from the child forms without affecting the base form. Now you have the answer why you should read this article. So, guys you can enjoy reading. IntroductionThe major change occurred for the VB programmers, when Microsoft announced the launch of their new VB.NET to become the best successor for our most favorite programming language Visual Basic. What VB lacks was the power of INHERITANCE, so Microsoft decided to implement inheritance in VB.NET. Every time we need a new form in our applications we create a new instance of the System.Windows.Forms.Form class, change its properties to suit our needs. Place some controls and our form is ready for use. As we know, by placing some controls on to a new form, we extend the forms class to NewForm1 means we have created a new class with a name NewForm1. Our new form is a class, we can extend any class due to inheritance supported in .NET. So from this we can conclude that we can design the base form and use the base form design in all our forms. Like we have, Base form: Public Class PMainForm
Inherits System.Windows.Forms.Form
Child forms: Public Class ChildForm
Inherits PmainForm
Now we can use the Child Forms in the ways we desired. Things we can do with our child forms: override the functionality of the base form within the child form itself (i.e. using Shadows keyword in our functions in the child form – we will also see this in details later in this article), we can add custom properties to our base form and set them in the child forms (for controlling the controls which originally does not exists in the parent form) and everything which we want. Concept of InheritanceI know that, OO Programmers must know each and everything of INHERITANCE. This section is especially for the non-OO based Programmers, so the experienced OO programmers can skip this section. Definition (Inheritance): Inheritance is the mechanism which allows a class A to inherit properties of a class B. We say "A inherits from B''. Objects of class A thus have access to attributes and methods of class B without the need to redefine them. The following definition defines two terms with which we are able to refer to participating classes when they use inheritance. Definition (Superclass/Subclass): If class A inherits from class B, then B is called superclass of A. A is called subclass of B. Objects of a subclass can be used where objects of the corresponding superclass are expected. This is due to the fact that objects of the subclass share the same behavior as objects of the superclass. Superclasses are called parent classes. Subclasses may be called child classes or just derived classes. Of course, you can again inherit from a subclass, making this class the superclass of the new subclass. This leads to a hierarchy of superclass/subclass relationships. If you draw this hierarchy you get an inheritance graph. A common drawing scheme is to use arrowed lines to indicate the inheritance relationship between two classes or objects. Visual InheritanceWe know how to create objects of classes. Just in a similar fashion, we can also inherit a form and I swear I am not kidding. Is it possible to inherit the components of the parent form? Yes, it is possible. But inheriting the controls does not means that we are able to view the controls in the child forms, because we inherits the controls but not the properties of those controls which we had set into the Parent form. Means suppose we have placed one Button in the Parent form. And its Top position is at 200px. This does not depicts that if we inherit the Parent Form for the child class then we will be able to see the same Button at the same position (i.e. Button1.Top = 200) So, how does this all happen? How we are able to see the controls of the parent form at the same location in the child form. VS.NET does this by repositioning the controls in the child form. You can experience this. Go and change the position in the Parent Form and don’t build the Parent form. Now open the Child Form and see the difference in the position of the Button, it is at the older position. Means, the position of the button has not changed even if we have changed it in the parent form. So what VS.NET does it resets the properties of the Controls in the Child Form, when we compile the Parent Form. Now you all know how VS.NET incorporates Visual Inheritance. What’s new in this articleIf you simply inherit the form, you will not be able to change the properties of the Controls which you have placed in the base form. This example covers:
Note: Protected Friend will be not listed there, so please take some pains and perform the following steps.
For full understanding of the concept you can download the accompanying application. This will provide you with the better view of forms inheritance. Well, that's it folks. Hopefully, this article will give the better understanding of how the forms inheritance works internally. And how avail the benefits of Visual Form Inheritance functionality of VS.NET. And please don’t forget to rate this article, so that I can continue writing, improve myself and present you something “best”. Feel free to ask me at saurabh.verma@india.rsystems.com for any queries. I will feel very great. You can read the whole article from the Readme.doc provided with the download. So, now go n enjoy the things. I will be back soon with some “best” code example for ADO.NET, till then take care. Bye.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||