Click here to Skip to main content
13,152,063 members (38,100 online)
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 9 Aug 2005

Dynamic Textbox

, 9 Aug 2005
Rate this:
Please Sign up or sign in to vote.
A dynamic TextBox that can be set to behave differently.


In one of my projects, I had to include TextBoxes 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.


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
    ''' An Enumerator which specifies all the 
    ''' properties of TextBox which '''are
    ''' the Type properties of TextBox. (It 
    ''' decides how the TextBox will '''behave?)
    Enum BoxType 
        Numeric = 0 
    End Enum 
    ''' An object of TextBox EnumType 
    '''to use in Type property
    Public objType As BoxType
    '''Property of TextBox using which the user 
    '''can set or get the type of TextBox
    Public Property Type() As String
            ''' It Returns the Type of TextBox 
            ''' when user requests NamOfTextBox.Type
            Return (objType.ToString) 
        End Get
        Set(ByVal Value As String)
            ''' It sets the Type of TextBox
            objType = Cint(Value) 
        End Set
    End Property
    '''' Key Press event of the TextBox which is fired 
    '''' when a user ''''types something in TextBox
    '''' Here it is decided that which function will be 
    '''' called according to ''''the type of the AbsTextBox
    '''' Depending on the type of TextBox a function is 
    '''' called which ''''accepts the event object of
    '''' OnKeyPress event.
    Protected Overrides Sub OnKeyPress(ByVal e As _
        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
    '''' To allow String characters only
    Private Sub txtStringValidate(ByVal e As _
        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
    '''' To allow Numeric characters only
    Private Sub txtIntegerValidate(ByVal e As _
        If (e.KeyChar >= "!" And e.KeyChar <= "/") Or _
                (e.KeyChar >= ":" And e.KeyChar <= "~") Then
            e.Handled = True
        End If
    End Sub
    '''' To allow String and Numeric characters only
    Private Sub txtMixedValidate(ByVal e As _
        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!


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Hungry Mind
Web Developer
India India
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,

Thank you.

You may also be interested in...

Comments and Discussions

Generaltry to build but failed Pin
Larry Sevilla12-Jul-07 3:08
memberLarry Sevilla12-Jul-07 3:08 
Generalwtf code Pin
Dani29200711-Jul-07 2:57
memberDani29200711-Jul-07 2:57 
GeneralRe: wtf code Pin
Dani2911-Jul-07 20:17
memberDani2911-Jul-07 20:17 
Generalpaste Pin
sprague2959-Aug-05 4:12
membersprague2959-Aug-05 4:12 
GeneralAbout regular expressions Pin
tkrafael_net9-Aug-05 2:59
membertkrafael_net9-Aug-05 2:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170924.2 | Last Updated 9 Aug 2005
Article Copyright 2005 by Hungry Mind
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid