Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
New Feature: Community Sites:
Create your own .NET community website and start earning from Google AdSense !
It's Free !
|
VB.NET development using Inheritance
Posted Date: 21 Nov 2007 Resource Type: Articles Category: .NET Framework
|
Posted By: Kumar Member Level: Silver Rating: Points: 10
|
Introduction Many a times during Visual Basic UI and its functionality development, developers face situations where some of the controls / layouts need to be replicated across all screens. Some of the ways to achieve that are:
* Create a base from, save and make a copy and rename it (external name and by modifying the .frm file in notepad) and then include it in the project * Copy-paste all the controls in the base form to the new form.
Of course, many more laborious ways are possible, but I’m not going to discuss them.
The above steps could give us visually similar forms to begin with, but a change in code in the base form would not reflect in the other. Similarly, a change in the UI in the base form would not be evident in the other form. There could still be a way out in terms of functionality – make all the form level functionality available in a module file and have both the base and the derived form access the functionality from the module. With functionality somehow taken care of, let’s turn to the UI – is there a way to synchronize that? No way!!
Too often, the requirements of the application have mandated development of controls that require a specific functionality. This has led to development of in-house ActiveX controls in the COM world or getting help from third-party vendors.
With the COM world gradually giving way to the .NET framework, the issues described above would still daunt developers. Is there a way out in .NET? The answer, fortunately, is a BIG Yes. Not only has .NET enabled complete object-oriented programming in Visual Basic, but also, the inheritance features can be extended to visual controls and forms. We can now inherit a complete form with all its UI and functionality into another from or we can create controls with a specific functionality and have forms inherit them. The article demonstrates inheritance features in .NET that can be applied to visual controls and forms.
Increased productivity and better standards adherence
Lets look at an example to better understand the issue. Lets say an application has the following requirements:
1. All its forms (screens) are to be of a uniform size. 2. There should be a Finish button on all screens, placed exactly 100 pixels from the bottom-right corner of the forms. 3. The button size has to be uniform throughout.
Given the fact the development is a distributed task with each developer getting some screens and a standards bible to adhere to, there is scope for some of the standards getting missed out during development. Of course, the problems may get caught during reviews but all that leads to extra time spent in making sure that the UI confirms to the standards.
.NET development helps developers follow standards and increase productivity per screen. In the above scenario in .NET, a base form with the required constraints needs to be developed. All other forms could inherit from the base form to get the same look and feel. Moreover, if there is a change in the form layout, it’s only the base form that needs changes and all other forms reflect the change.
Lets look at another example of a decimal text box. A decimal text box should ensure that only numbers, backspace and just one ‘.’ be accepted as input. The way to do it in COM is to create an ActiveX control out of a text box. The process is a bit painful. .NET has again made the task of creating a custom control easier. One can create a Windows Control Library for use across all .NET application or can create a custom control within an application and have all forms use it.
Get started with Code
Lets get going with code to have a real good understanding what’s detailed above. I’ll walk you through an application where we’ll create a custom control called DecimalTextBox and a base form called BaseForm, which has the Finish button, appropriately placed, and also has the DecimalTextBox as one of the controls. We’ll have all other forms inherit from BaseForm and see how easy inheritance makes it to improve standards following and productivity.
Needless to say, you need to have the Visual Studio.NET Beta2 SDK installed.
* Create a new Visual Basic.NET Windows Application. Name it as you wish. I’ll call it WinApp. * Delete Form1.vb from the project and add a new form to the project. Name is BaseForm.vb * Lets place a button on the base form and name it btnFinish (again conforming to coding standards!). Place it in the bottom-right corner as the requirements mandate. Now, nobody told me if the text on the button should always be Finish. What if the user wants to make it End in one of the derived forms? Lets go ahead and create a FinishButonText form level property so that user can change the button text at runtime or design time (visible in property pages). In order to have a property visible during design time and give it some description, we need to use a couple of attributes (new in .NET) for the property. The attributes (nothing but classes) are available in System.ComponentModel namespace, so we have to import it into our form. Here goes the code:
Imports System.ComponentModel …. …. Public Class BaseForm Inherits System.Windows.Forms.Form
<Browsable(True), Description("Text for Finish Button")> _ Public Property FinishBtnText() As String Get FinishBtnText = Me.btnFinish.Text End Get Set(ByVal Value As String) Me.btnFinish.Text = Value End Set End Property End Class
The Imports statement has to be the topmost statement in the code. The Browsable attribute set to TRUE indicates that the property is visible in the property pages. The Description attribute is what comes as help text when a user clicks the property.
Note that the Text property of the Finish button itself cannot be modified in the inherited forms and hence a new form level property FinishBtnText to modify the button text.
* Now lets create a new user control by adding one to the application. Name is DecimalTextBox.vb. Since we need to inherit from a TextBox, going to the code pane of the control, we make the following change:
Change Inherits System.Windows.Forms.UserControl To Inherits System.Windows.Forms.TextBox
* Since we have to prevent users from entering a non-numeric and more than one “.” from being entered, we associate a new EventHandler to the control to handle out requirements. Code is provided below(copy the highlighted code in the already existent New method):
Public Sub New() MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call AddHandler DecimalTextBox.KeyPress, AddressOf key_press End Sub
The AddHandler method takes the control and its event we are adding the handler to as the first parameter and a delegate for the event handler.
* Now we add a method key_press, which take parameters as the object calling the method and the event arguments associated with the event, i.e., KeyPressEventArgs
Private Sub key_press(ByVal sender As System.Object, ByVal e As KeyPressEventArgs) Dim dtb As New DecimalTextBox() dtb = sender If Char.IsDigit(e.KeyChar) Or Asc(e.KeyChar) = 8 Or Asc(e.KeyChar) = 46 Then If Asc(e.KeyChar) = 46 And InStr(dtb.Text, ".") > 0 Then e.Handled = True Else e.Handled = False End If Else e.Handled = True End If End Sub
Handled set to TRUE indicates that the event has been handled and Windows need not execute the normal event handling routine for such an event.
* We are done with the coding for the control event but still have to make the control visible in the left-side toolbox pane for use in the base-form. In order to accomplish this, we need to compile the application. Once this is done, DecimalTextBox appears in the toolbox pane and could be dragged-dropped into BaseForm. (If you get an error about form1 being unavailable, go to Project properties and set the start up form as BaseForm and compile again. Form1 was deleted in the beginning)
* Lets drag-drop one DecimalTextBox control, place a label in front of it asking user to enter a decimal number, into BaseForm. Once done, we need to re-compile the code to make the form available as an inherited form in its changed form.
* Now add a new form, but an inherited one! Go to Project menu and select Add Inherited form…. Once you select a name for the form, you’ll be taken to the inheritance picker dialog box. Notice BaseForm there? Select it and click OK. Notice that the new form now has the same UI as the BaeForm. Also note that the controls added to BaseForm are not editable (nor movable) and that is the reason a new form level property was added to edit the button’s name. You could certainly add code to the inherited control events to take care of any screen level functionality.
* Lets now make a change to the BaseForm – add a new TextBox? Add it and re-compile the code. Go to the new form and notice that the newly added TextBox is present. That’s the power of inheritance not available to VB programmers before the advent of .NET!!
* Go to project properties again and set the start-up form as the new form. Run (F5) the application and enter a decimal number to the text box. You’ll notice that the text box behaves as it was supposed to. Moreover, the same textbox can be inherited in all the other forms we create in the application, independent of the BaseForm and the functionality would still be there
Conclusion
.NET has made it easy for Visual Basic developers to fully make use of object-oriented features, not only in a standard way, but also in terms of visual controls. This would certainly lead to better productivity and ease of use from a language already recognized as one of the easiest to code in.
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|