Everything You Wanted to Know About Forms Inheritance in VB.Net - What's New in This Article, Cont'd
(Page 5 of 6 )
4) How you 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 <Control> as return type.
'InfoLabel Property (Control which we want to use from the child form)
Dim localLabel As Label
Protected 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 ControlProperty from the Child Form for the parent form, in any event of the child form. For this example I have done this in the load event of the child form.
Private Sub CForm3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.InfoLabel = Label1 'Control to be controlled by the Parent Form
Me.Position = 99
End Sub
5) How to override the functionality of the Parent form from the Child Form
Override the function of the parent form, which handles the particular functionality (for example, the click event of the button in the parent form).
If you copy and paste that function from the Parent Form, please don’t forget to add the Shadows Attribute, for suppressing the function of the Parent Form (Note: when we use the Shadows keyword, then there is no need to use the Overridable keyword in the Parent Form for that function).
Parent Form:
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.)
This is one of the main problems when we inherit any ParentForm – trust me, its very simple. Change the Modifiers property of the control, we want to control from the Child Form from Friend to Protected Friend.
Protected Friend will be not listed there, so please take a deep breath and perform the following steps:
1) Open the Code Window for the Parent Form.
2) Expand "Windows Form Designer generated code" section.
3) Find InitializeComponent() function.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
4) Above this function you will find the initialization code for the controls which we have been placed into the form. They will be like:
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents PBNavPrev As System.Windows.Forms.Button
Friend WithEvents NavCaption As System.Windows.Forms.Label
Friend WithEvents BParent As System.Windows.Forms.Button
Friend WithEvents NavPanel As System.Windows.Forms.Panel
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Change the access modifiers of the control to Protected Friend for sharing them in the child form.
5)
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Protected Friend WithEvents Button1 As System.Windows.Forms.Button
Protected Friend WithEvents PBNavPrev As System.Windows.Forms.Button
Protected Friend WithEvents NavCaption As System.Windows.Forms.Label
Protected Friend WithEvents BParent As System.Windows.Forms.Button
Protected Friend WithEvents NavPanel As System.Windows.Forms.Panel
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Next: Conclusion >>
More VB.Net Articles
More By Saurabh Verma