Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake a TextBox that allows only digits in VB.NET
DescriptionThis example shows how to make a TextBox that allows only digits in VB.NET. It creates a TextBox subclass that sets its extended style to allow only digits. It also overrides the class's WindowProc to ignore paste messages.
KeywordsTextBox, lower case, VB.NET
CategoriesControls, VB.NET
 
The program creates a DigitTextBox class that is a subclass of TextBox. The class's constructor uses GetWindowLong and SetWindowLong to set the control's ES_NUMBER extended style bit. (It's too bad the TextBox class doesn't have GetExtendedStyle and SetExtendedStyle methods.)

This class also overrides the TextBox's WndProc subroutine to inspect Windows messsages. It ignores any WM_PASTE and WM_CONTEXTMENU messages so the user cannot press Ctrl-V or use the TextBox's context menu to paste non-numeric text into the control.

 
Private Declare Function SetWindowLong Lib "user32" Alias _
    "SetWindowLongA" (ByVal hWnd As IntPtr, ByVal nIndex As _
    Int32, ByVal dwNewint32 As Int32) As Int32
Private Declare Function GetWindowLong Lib "user32" Alias _
    "GetWindowLongA" (ByVal hWnd As IntPtr, ByVal nIndex As _
    Int32) As Int32
Private Const GWL_STYLE As Int32 = (-16)
Private Const ES_NUMBER As Int32 = &H2000

Private Class DigitTextBox
    Inherits TextBox

    Public Sub New()
        ' Get the current style.
        Dim style As Integer = _
            GetWindowLong(Me.Handle, GWL_STYLE)

        ' Add ES_NUMBER to the style.
        SetWindowLong(Me.Handle, GWL_STYLE, _
            style Or ES_NUMBER)
    End Sub

    ' Override the WndProc and ignore the
    ' WM_CONTEXTMENU and WM_PASTE messages.
    Protected Overrides Sub WndProc(ByRef m As _
        System.Windows.Forms.Message)
        Const WM_CONTEXTMENU As Int32 = &H7B
        Const WM_PASTE As Int32 = &H302

        If (m.Msg <> WM_PASTE) And (m.Msg <> _
            WM_CONTEXTMENU) Then
            Me.DefWndProc(m)
        End If
    End Sub
End Class
 
When the form loads, it creates a new DigitTextBox control and initializes its properties.

The program also sets the ES_NUMBER property on TextBox2 so you can see the difference. While you cannot type a non-digit into TextBox2, you can copy and paste one into it.

 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    ' Create the DigitTextBox.
    DigitTextBox1 = New DigitTextBox
    DigitTextBox1.Name = "DigitTextBox1"
    DigitTextBox1.Location = New Point(TextBox1.Left, _
        Label3.Top)
    DigitTextBox1.Size = TextBox1.Size
    DigitTextBox1.TabIndex = 2
    DigitTextBox1.Text = ""
    DigitTextBox1.Font = TextBox1.Font
    Me.Controls.Add(DigitTextBox1)

    ' Set the numeric style for TextBox2.
    ' Get the current style.
    Dim style As Integer = _
        GetWindowLong(TextBox2.Handle, GWL_STYLE)

    ' Add ES_NUMBER to the style.
    SetWindowLong(TextBox2.Handle, GWL_STYLE, _
        style Or ES_NUMBER)
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated