954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

NumberBox - TextBox that takes only number input

0
By Niche Reyes on Mar 15th, 2011 4:37 am

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

simply use "isnumeric"

If Not (IsNumeric(NumBox1.Text)) Then

            MsgBox("put only numbers")


        End If
abhay1234
Light Poster
46 posts since Nov 2009
Reputation Points: 10
Solved Threads: 5
 

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"

PdotWang
Newbie Poster
20 posts since Mar 2011
Reputation Points: 27
Solved Threads: 5
 
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
codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

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

Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
 

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.

PdotWang
Newbie Poster
20 posts since Mar 2011
Reputation Points: 27
Solved Threads: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: