NumberBox - TextBox that takes only number input

Updated ndeniche 0 Tallied Votes 2K Views Share

This snippet is to create a user control that will only take numbers. Numbers will accept decimal values, for which the NumberBox is validated to take only one.

The implementation for the control is the same as it is for the TextBox, since it is inherited from this class and uses all of its attributes and methods:

Private Sub InitializeComponent()
    'Initialize Object with the constructor for the class
    '[UserControls] is the Library in which the class is created
    Me.NumBox1 = New UserControls.NumberBox()
    '
    'NumBox1
    '
    Me.NumBox1.Location = New System.Drawing.Point(45, 75)
    Me.NumBox1.Name = "NumberBox"
    Me.Numbox1.Size = New System.Drawing.Size(100, 20)
    Me.NumBox1.TabIndex = 0
    '
    'Form1
    '
    'Add Control to form
    Me.Controls.Add(Me.NumBox1)

End Sub
Friend WithEvents NumBox1 As UserControls.NumberBox
Imports System.Windows.Forms

Public Class NumberBox
Inherits TextBox

Private Sub KeyDown_Handler(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles Me.KeyPress
If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) Then
e.Handled = True
End If

If e.KeyChar.ToString().Equals(".") Then
If Me.Text.Contains(".") Then
e.Handled = True
Else
e.Handled = False
End If
End If
End Sub

End Class
abhay1234 0 Light Poster

simply use "isnumeric"

If Not (IsNumeric(NumBox1.Text)) Then

            MsgBox("put only numbers")


        End If
PdotWang 17 Newbie Poster

A very useful snippet.

You may also want to add cases of "1e-10", "-10.99", etc.
Some accounting people even use "(10.99)" for "-10.99"

codeorder 197 Nearly a Posting Virtuoso
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        Select Case e.KeyChar
            Case "1"c, "2"c, "3"c, "4"c, "5"c, "6"c, "7"c, "8"c, "9"c, "0"c, CChar(vbBack) '// your #'s and Backspace.
                e.Handled = False '// allow.
            Case "."c '// Decimal dot.  allow only one.
                If Not TextBox1.Text.Contains(".") Then e.Handled = False Else e.Handled = True
            Case "-"c '// Minus/Subtract sign.  allow only as first char.
                If TextBox1.SelectionStart = 0 AndAlso Not TextBox1.Text.Contains("-") Then e.Handled = False Else e.Handled = True
            Case Else
                e.Handled = True '// block.
        End Select
    End Sub
ndeniche 402 Posting Virtuoso Featured Poster

ooh... totally forgot about the minus sign :P... Thanks for the collaboration

PdotWang 17 Newbie Poster

simply use "isnumeric"

If Not (IsNumeric(NumBox1.Text)) Then

            MsgBox("put only numbers")


        End If

The problem with the following code is that, it may show too many times to the user.

If Not (IsNumeric(NumBox1.Text)) Then
MsgBox("put only numbers")
End If

If you use a Msgbox, you'd better not to put it in any key event. You use it in Validating event or LoseFocus event. But I think this post provide a better way, more user friendly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.