Sunday, January 29, 2012

inherit control in asp.net

It is the control which id inherited from an existing control and providing additional functionalities in it.To create inherited user control the class declaration need to be as below.
ex:Masked Text box of Rich Text box which are inherited from a tex box control
public class MaskedTextBox :TextBox 
public class RichTextBox:TextBox 
Creating a mytextbox control which can be customised in such a way it can accept the only numeric or characters or both numeric of characters as well anything.Open the c# control project wha we developed previously and add a new class to it naming it as mytextbox.cs and write the following code in it.

using system,windows.Forms;
namespace inherited controls
{
public class mytextbox:TextBox
{
public ENUM options{Digits,Any,Chars,char or digit};
options opt=options.Any
public myTextbox()
{
This.keypress+=new KeyPressEventHandler (myTextBox-keypress);
}
private void mytextbox_keypress(object sender,keypressevent args e)
{
if(convert.toInt32(e.keychar==8)
return;
switch(opt)
{
case option.Digits;
if(char.IsDigit(e.keychar)==false)
{
messagebox.show("enter numbers only");
e.Handled=true;
}
break;
case options.cahrs
if(char.Isletter(e.keychar)==false)
{
{
messagebox.show("enter numbers only");
e.Handled=true;
}
break;
case options.cahrs or digit
if(char.Isletter or digit(e.keychar)==false)
{
{
messagebox.show("enter numbers and chars only");
e.Handled=true;
}
break;
} 
}
public option setoption
{
get{returns opt;}
set(opt= value;}
}
}
} 
} 

No comments:

Bel