|
|||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Table of contents
What is the need of Visual Inheritance?So, the biggest question before reading this article - what is the need of Visual Inheritance. We will need visual inheritance when we want to provide some base functionality to our application. First, we will be coding all the base/common code into the Base Form (any We can inherit the base form into the form where we want to implement the common functionality. As we inherit the form, all the For example: in a common scene, we have to design navigation panel in our application to navigate from one record to another. Previously, to avoid the repeatable code, we use ActiveX Control. First, we design the ActiveX Control for the navigation and then we place this onto our form where we want 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 functionality 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 our project, we 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 can also override some/all functionality of the Base forms from the child forms without affecting the base form. From now onwards, Visual Inheritance and Form Inheritance can be used interchangeably. 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 application, we create a new instance of the 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 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 Is it possible to inherit the components of the parent form? Yes, it is possible. But inheriting the controls does not mean that we are able to view the controls in the child forms, because we inherit the controls but not the properties of those controls which we had set into the Parent form. Means, suppose we have placed one So, how does this all happen? How are we 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 Means, the position of the button has not changed even if we have changed it in the parent form. So what VS.NET does is, 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 article?If you simply inherit the form, you will not be able to change the properties of the controls that you have placed in the base form. This example covers:
1) How to inherit the form?Open the child form and replace the Child form’s <System.Windows.Forms.Form>
Base Form:Public Class PMainForm
Inherits System.Windows.Forms.Form
Child Forms:Public Class ChildForm
Inherits PmainForm
2) How to make custom properties?Open the Parent Form and create a property (having a scope as 'CurrentPosition Property
Dim LPosition As Long
Protected Property Position() As Long
Get
Return LPosition
End Get
Set(ByVal Value As Long)
LPosition = Value
End Set
End Property
3) How to use the custom properties from the Child form?Open the child form. Use Private Sub CForm3_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.Position = 99
End Sub
4) How to change the properties of the controls from the parent form, which are not in the parent form (i.e. they are in the Child forms)?Open the Parent Form, define a property which accepts 'InfoLabel Property (Control which we want to use from the child form)
Dim localLabel As LabelProtected Property InfoLabel() As Label
Get
Return localLabel
End Get
Set(ByVal Value As Label)
localLabel = Value
End Set
End Property
Now, open the child form. Place the control, which you want to use with the Parent Form. Change the Private Sub CForm3_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'Control to be controlled by the Parent Form
Me.InfoLabel = Label1
Me.Position = 99
End Sub
5) How to override the functionality of the Parent form from the Child form?Override that function of the parent form, which handles the particular functionality (For e.g. If you copy and paste that function from the Parent form, please don’t forget to add the Parent Form:'How to share the implementation of the Parent Form
Protected Friend Overridable Sub BParent_Click(ByVal _
sender As System.Object, ByVal e As System.EventArgs)_
Handles BParent.Click
MsgBox("Functionality of Parent is used.")
End Sub
Child Form:'Overiding the implementation of the Parent Form
'please don't forget to remove the handles keyword from the last while
'implementing in the child form
Protected Friend Overrides Sub BParent_Click(ByVal _
sender As System.Object, ByVal e As System.EventArgs)
MsgBox("Functionality of Child is used.")
End Sub
Or Protected Friend Shadows Sub BParent_Click(ByVal _
sender As System.Object, ByVal e As System.EventArgs)
MsgBox("Functionality of Child is used.")
End Sub
6) How to change the properties of the inherited controls at design time? (I.e. how to reposition the controls, set different properties, etc.)The main problem, when we inherit any Parent form - it's very simple. Change the
For full understanding of the concept, you can download the accompanying application. This will provide you with a better view of form inheritance. Well, that's it folks. Hopefully, this article will give a better understanding of how forms inheritance works internally. And how to 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 saurabhdotnet@hotmail.com for any queries. I will feel very great. So, now go and enjoy the things. I will be back soon with some "best" code example for ADO.NET, till then take care. Bye.
|
||||||||||||||||||||||||||||||||||||||||||||