Reader Level:
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.
  • 0
  • 0
  • 18458

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

COMMENT USING

Trending up