Introduction
In one of my projects, I had to include TextBox
es which allow only numeric characters, string characters or both without special characters. I did that, but I had to write functions for each TextBox
or make a class and call these functions for the TextBox
validation. So, I came up an idea to develop a multipurpose TextBox
which can be set to allow only string characters or numeric characters, or mixed string and numeric characters, where there will be no need to write or call functions to do the validation.
Background
I searched on the net for help so that I could get some basic idea about this type of dynamic TextBox
. I got all the validation functions but no one could help me to connect all these functions and make the TextBox
dynamic. After studying the properties and Enum
in VB.NET, I thought why not let the user decide the type of TextBox
he/she wants to use.
The solution is fairly simple, and it's very easy to implement in your project/application. Here is a synopsis of what we will be doing to develop a dynamic TextBox
:
- Create a Windows control library project.
- Copy paste all the code given below one by one.
- Build your project (which will make a DLL in the project's directory).
- Add the reference from other projects to this DLL.
- Add this control to the Toolbox by using Add/remove items.
- Set the type of the
Textbox
you want (numeric, text, or mixed).
Using the code
The first step required to develop a dynamic TextBox
is to create a new Windows control library project from the Visual Studio .NET template. Save it with a name of your choice. In the .VB file, you will get a class with the name of your project. Now, replace the entire code of the class with the code given below:
Imports System
Public Class DynamicTextBox
Inherits System.Windows.Forms.TextBox
Enum BoxType
Numeric = 0
Text
Mixed
End Enum
Public objType As BoxType
Public Property Type() As String
Get
Return (objType.ToString)
End Get
Set(ByVal Value As String)
objType = Cint(Value)
End Set
End Property
Protected Overrides Sub OnKeyPress(ByVal e As _
System.Windows.Forms.KeyPressEventArgs)
Select Case objType
Case objType.Numeric
Call txtIntegerValidate(e)
Case objType.Text
Call txtStringValidate(e)
Case objType.Mixed
Call txtMixedValidate(e)
End Select
End Sub
Private Sub txtStringValidate(ByVal e As _
System.Windows.Forms.KeyPressEventArgs)
If (e.KeyChar >= "!" And e.KeyChar <= "@") Or _
(e.KeyChar >= "[" And e.KeyChar <= "_") Or _
(e.KeyChar >= "{" And e.KeyChar <= "~") Then
e.Handled = True
End If
End Sub
Private Sub txtIntegerValidate(ByVal e As _
System.Windows.Forms.KeyPressEventArgs)
If (e.KeyChar >= "!" And e.KeyChar <= "/") Or _
(e.KeyChar >= ":" And e.KeyChar <= "~") Then
e.Handled = True
End If
End Sub
Private Sub txtMixedValidate(ByVal e As _
System.Windows.Forms.KeyPressEventArgs)
If (e.KeyChar >= "!" And e.KeyChar <= "/") Or _
(e.KeyChar >= ":" And e.KeyChar <= "@") Or _
(e.KeyChar >= "[" And e.KeyChar <= "`") Or _
(e.KeyChar >= "{" And e.KeyChar <= "~") Then
e.Handled = True
End If
End Sub
End Class
Now build your project and create a new project to test the DLL and add the reference of the generated DLL in your project, and add the control (DLL) to the Toolbox using Add/Remove items. Select the .dll file from the project directory and drop the TextBox
on the form and now open the property window of the TextBox
and set the Type
property to numeric or Text or Mixed. Now execute your Test project and see the magic of this dynamic control.
Happy programming and best of luck!
I m a Graduate in Computer Science.
My hobies are to scratch new things from it's bottom and develop new thigs which can be useful for me and others.
So i got the Software developement the best stream where u can help others and u will be pampered with everything.
When i watch someone happy then i feel like that person is sharing my happiness and u know if someone is happy with u'r matters then that is the true person beyond everything for u.
So plz help others but first try to help u'r self then only u will enjoy helping others.
Good Luck.....
Please visit to my web space for some more articles,
http://www.FamilyCode.com
Thank you.