Introduction
This article describes how we can
develop TextBox Validation properties with Class library. We are going
to see how to add properties to Textbox control. Here I will discuss
how to add basic validation property.A class is created which inherits
Textbox control.
we can either add methods or properties to
our class. Once we have finished with it, compile the class to create
Textbox control with additional properties and methods which you would
like to include in it.
I have discussed in two parts , the second
one is just the extension of first one.In first part we will see how
validation is done for numbers and characters.
second part
consists of setting the property for Numcheck and Charcheck, the
Textbox control which we are developing overides OnKeyPress event. If
you have set True for Numcheck then the Textbox allows only numbers to
type.
Background:
I was writing validation procedures in each form for all textboxes
at first when I was learning .Net. Then later on wrote all validation
methods in a class and created its object and used those methods
effectively.
I thought why not to add at least some properties or methods to existing Textbox control and use it in my project.
How To:
Part 1:
Creating Textbox control with validation properties
1) In Visual Studio create a Class Library project (Not Windows control library template).
2) By default a class named class1.cs is created you change its name
to say RTextbox (the name which I have used in demo project).
3) Click on .cs file and change the name of the class inside it to RTextbox.
4) The following dll need to be added under project references in order to get Textbox control
System.Windows.Form.dll
5) Add namespace using System.Windows.Form in RTextbox.cs class.
6) Change the RTextbox class definition so that it inherits TextBox class.
public class RTextbox : Textbox
Using Code:
The following are the validation properties which are included in demo project.
Note: you can either make it as a property with get method only or you can use method instead.
Numcheck property:
This property is used to check whether the text entered in Textbox is a valid number or not.
public bool Numcheck
{
get
{
try
{
Convert.ToInt64(this.Text);
return true;
}
catch(Exception ex)
{
return false;
}
}
}
Charcheck property:
This property is used to check whether the text entered in Textbox contains valid characters or not.
public bool Charcheck
{
get
{
string good="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\n\r ";
for(int i=0; i<this.Text.Length; i++)
{
if( good.IndexOf(this.Text[i]) == -1 )
return false;
}
return true;
}
}
Here is the code snippet for RTextbox class.
using System;<br />using System.Windows.Forms;<br />using System.Drawing;<br />namespace RTextbox<br />{<br /> public class RTextbox : TextBox { public RTextbox() { //
// TODO: Add constructor logic here
// }
public bool Numcheck
{
…...
…...
}
public bool Charcheck
{
…...
…...<br /> } }<br />}
Compile the project. Copy RTextbox.dll from bin folder to your project where you want to use.
How to use RTextbox control in your project:
1) Add RTextbox.dll to your project's bin directory.
2) Under project references add RTextbox.dll.
3) Right Click on Tool bar click on Add/Remove Items , then add RTextbox.dll you will find the control in Toolbar.
4) Drag and drop the control to your form.
Note: click on properties
you will see 2 properties Numcheck and Charcheck in addition to other
properties and methods of Textbox control.
Part 2: We have seen how to inherit TextBox control. Now we will see how we can override
OnKeyPress event of Textbox control in RTextbox class. The following are the
properties which are defined; you need to set those properties to True if
validation is required.
By default those properties are set to false. We have 2
properties
1) Numcheck: when set true checks whether text entered in Textbox is
a valid number or not.
2) Charcheck: when set true checks whether text entered
in Textbox is a valid characters or not.
Note: the implementation for Numcheck
and Charcheck are not done in property, it's handled in OnKeyPress event. We are
overriding OnKeyPress event in RTextbox.cs class. Check whether Numcheck
property is set true, if so validate key pressed for Number
OnKeyPress Event for Charcheck:
if ( char.IsLetter(e.KeyChar) == false )
{
if ( e.KeyChar.Equals(' '))
e.Handled=false;
else
if( e.KeyChar.Equals(Convert.ToChar(Keys.Back)))
e.Handled=false;
else
e.Handled=true;
}
The above code is for Validating text
entered in Textbox. It allows only characters, blank spaces. It also handles
backspace key press
OnKeyPress Event for Numcheck:
if ( char.IsNumber(e.KeyChar) == false )
{
if( e.KeyChar.Equals(Convert.ToChar(Keys.Back)))
e.Handled=false;
else
e.Handled=true;
}
The above code is for validating text
entered in Textbox. It allows only Numbers, no need to include blank spaces. It also handles
backspace key press
How to use RTextbox.dll in your project(Part-2):
1) Copy past RTextbox.dll to your Project bin
directory
2) Right click references click
on Add Reference from Solution window, Click on Browse button, select
RTextbox.dll from Bin directory. Click on Ok button.
3) Right click on Toolbar, click on Add/Remove
items. Click on browse button and select RTextbox.dll. You will find RTextbox
control in your Tool bar.
4) Drag and
drop RTextbox control to your project Form, right click on RTextbox object, you
will see 2 more addition properties Charcheck and Numcheck.
5) Set Numcheck property to True for Number
validation, it allows only numbers.
Points of interest:
I find it very nice after creating my Textbox control and used it in
my projects.You can also override the methods of Textbox control so
that you can use the control with your code in your won style. you will
feel how good your control your validation for Textbox are all
encapsulated in your control.
Conclusion:
We have learnt how we can create our own
Textbox Control which extends System.Windows.Form Textbox control. How
to add validation properties , handling key press event for Textbox.