Inheritance and Windows Forms - Explanation
(Page 5 of 5 )
As you can tell, we overrode the sub in the base class. So that when the user click the “Add” button, on the “ManageEmployee” form, our “ManageManagers” form will take over, and do its work. Note that we commented the “Handles btnAdd.Click.” Why?
Good question. Remember that we are overriding a sub in the parent class, and the event is being captured on the parent class, so in the child class we don’t receive the event. Rather, it is being received by the parent class.
Since the first thing we want to do before creating a new manager is to validate the user inputs, and since the function that validates the user inputs is in the parent class “ManageEmployees”, then we will call our parent “IsValidInputs” which will determine if the inputs are valid and if we can go on with our work. So the “btnAdd_Click” sub will just look as beautiful as this:
Protected Overrides Sub btnAdd_Click
(ByVal
sender As System.Object, ByVal e As
System.EventArgs) 'Handles btnAdd.Click
Dim oManager As Manager
Try
If IsValidInputs() Then
oManager = New Manager(MyBase.txtName.ToString,
Integer.Parse(MyBase.txtAge.ToString))
End If
Catch oException As Exception
'Handle any unexpected errors here
End Try
End Sub
Always remember that any change on the parent form “ManageEmployee” should be followed by a “Build” to the project in order to carry out the changes on the inheriting forms.
This is just a glance on how to use the windows forms and the inheritance between forms: we wrote the general code on a parent form, and then used it with the child forms. Now if we want to add more fields, add extra functionality, change some logic, or even play with the properties of the form - so we only need to change it on the parent form - build the project, and the changes will automatically be reflected on the children forms.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |