65.9K
CodeProject is changing. Read more.
Home

Dynamic Textbox

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.17/5 (9 votes)

Aug 9, 2005

2 min read

viewsIcon

74862

downloadIcon

973

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:

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!