SIGN UP MEMBER LOGIN:    
ARTICLE

Visual Inheritance in C#-Part1

Posted by Prasad H Articles | C# Language September 24, 2001
We all know that Inheritance means a extending a class with more Features without worrying about the implementation of features of hidden inside the class to be inherited.
Reader Level:

Description :

We all know that Inheritance means a extending a class with more Features without worrying about the implementation of features of hidden inside the class to be inherited. Let's work through a running example of inheritance. The TextBox class under System.Windows.Forms is control to enter TextBox it hides from the developers how the text appears when we enter some text how it behave when deleted all these stuffs are hidden from the developer.Let's use this feature of TextBox and put some restriction on the characters to be entered in the TextBox by inheriting from the TextBox class.

Let's Construct a useful TextBox which should only accept numbers:

So when we need the many textbox to accept only number what we can do is simply copy the class NumberBox below and use : 

NumberBox n1=new NumberBox() instead of TextBox after this the NumberBox

Will behave just like TextBox but will only accept numbers.

How this is achieved.This functionality is achieved by making use of inheritance: 

class NumberBox:TextBox

this statement NumberBox should have all the features of TextBox.To make the textbox to accept only numbers what is done the keypress event is written for this textbox so whenever the object of NumberBox is created the KeyPress event is built into that to make use of the Number validation so it is not necessary for the user to write code for each and every textbox to be validated for numbers. In the KeyPressEventHandler when the Handler property of the KeyPressEventArgs is set to true the key will be masked. 

Features to be added:ErrorProvider control can also be added inside the class so that on pressing non-numeric key we can make the error provider to appear. This code is easily modified to make Textbox to accept only Alphabets Or AlphaNumeric etc....

Full Source Code:

// Source Code starts
using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
class NumberBox:TextBox
{
public NumberBox()
{
this.KeyPress+=new KeyPressEventHandler(NumberBox_KeyPress);
}
private void NumberBox_KeyPress(object sender,KeyPressEventArgs kpe)
{
int KeyCode=(int)kpe.KeyChar;
if(!IsNumberInRange(KeyCode,48,57) && KeyCode!=8 && KeyCode!=46)
{
kpe.Handled=
true;
}
else
{
if(KeyCode==46)
{
kpe.Handled=(
this.Text.IndexOf(".")>-1);
}
}
}
private bool IsNumberInRange(int Val,int Min,int Max)
{
return (Val>=Min && Val<=Max);
}
}
class NumberBoxDemo:Form
{
Label l1=
new Label();
NumberBox n1=
new NumberBox();
public NumberBoxDemo()
{
l1.Text="Number Box:";
n1.Location=
new Point(l1.Left+l1.Width+10,l1.Top);
this.Controls.Add(l1);
this.Controls.Add(n1);
}
public static void Main()
{
Application.Run(
new NumberBoxDemo());
}
}
// Source Code End

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Become a Sponsor