C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


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 !




Visual Inheritance in C#


Posted Date: 01 Jul 2006    Resource Type: Articles    Category: .NET Framework
Author: Abhishek AryaMember Level: Diamond    
Rating: Points: 10



Introduction


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

The 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…..

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




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Usefull COM and COM +Questions
Previous Resource: Collections in VB.Net
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use