Click here to Skip to main content
15,891,431 members
Articles / Programming Languages / Visual Basic
Article

Dynamic Textbox

Rate me:
Please Sign up or sign in to vote.
3.17/5 (9 votes)
9 Aug 20052 min read 74.3K   970   18   5
A dynamic TextBox that can be set to behave differently.

Introduction

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.

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:

VB
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 
        Text 
        Mixed 
    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
        Get
            ''' 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 _
                   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
    
    '''' To allow String characters only
    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
    
    '''' To allow Numeric characters only
    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
    
    '''' To allow String and Numeric characters only
    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!

License

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


Written By
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,




http://www.FamilyCode.com




Thank you.

Comments and Discussions

 
Generaltry to build but failed Pin
Larry Sevilla12-Jul-07 3:08
Larry Sevilla12-Jul-07 3:08 
Generalwtf code Pin
Dani29200711-Jul-07 2:57
Dani29200711-Jul-07 2:57 
GeneralRe: wtf code Pin
Dani2911-Jul-07 20:17
Dani2911-Jul-07 20:17 
Generalpaste Pin
sprague2959-Aug-05 4:12
sprague2959-Aug-05 4:12 
GeneralAbout regular expressions Pin
Rafael Nicoletti9-Aug-05 2:59
Rafael Nicoletti9-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.