Crib Sheets
Home Page
Calculator
Chemistry
Convert Units
Electronic
First Aid
Geography
Geology
Mathematics
Music
Nautical
Numbers
Physics
VB.net Programming
Dates Date Example Maths Text Numeric Textbox

Weather
Web Programming
Wiring
DigitalDan Websites
DigitalDan Sites
Hit Counter 113
 
©2000-2013 DigitalDan.co.uk
 
Contact Details
Email address scroller
DigitalDan.uk
VB.NET Numeric Textboxes

 
The functions outlined below are written in VB.net but it should be possible to modify them for other .net languages.
This may need minor adjustments if you use special compiler options (Option Strict) because I have tried to simplify the code for ease of understanding

How to create an "Integer" number only textbox
Picture of completed project (The MaximumLength property limits the number of letters/digits that a user can type into your text box)
Private Sub txtNumber_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtNumber.KeyPress
   If e.KeyChar = Chr(8) Then Exit Sub
   If Not "1234567890".Contains(e.KeyChar) Then e.Handled = True
End Sub
When reading our textbox, we must remember that it is a textbox - it expects to give us a string (letters) but we want to know what digits (number) is it displaying.
This code demonstrates how to read our txtNumber textbox
   Dim number As Integer
   number = CInt(Val(txtNumber.Text))
  • txtNumber.Text tells us what the textbox is displaying
  • Val(....) looks at the text and converts it into a decimal number (a number with a decimal point e.g 12.345)
  • CInt(....) converts the decimal number into a normal integer number (one that does not have a decimal point)

  • How to create a "Decimal" number only textbox
    Picture of completed project
    Private Sub txtNumber_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtNumber.KeyPress
       If e.KeyChar = Chr(8) Then Exit Sub
       If Not "1234567890.".Contains(e.KeyChar) Then e.Handled = True
       ' There is a decimal point inside the string 1234567890.
       If e.KeyChar = "." And txtNumber.Text.Contains(".") Then e.Handled = True
       ' You cannot have a number with two decimal points
       ' if user pressed "." and the number already has a "." then do not allow the second "."
    End Sub
    
    This code demonstrates how to read our txtNumber textbox
       Dim number As Double
       number = Val(txtNumber.Text)
    

    How to create an "Integer" number only textbox that allows negative numbers
    Picture of initial project Your form will now look like this
    Picture of completed project
    Private Sub txtNumber_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtNumber.KeyPress
       If e.KeyChar = Chr(8) Then Exit Sub
       If Not "1234567890+-".Contains(e.KeyChar) Then e.Handled = True
       If e.KeyChar = "+" Then
          txtMinus.Text = "+"
          e.Handled = True
       End If
       If e.KeyChar = "-" Then
          txtMinus.Text = "-"
          e.Handled = True
       End If
    End Sub
    
       Dim number As Integer
       number = CInt(Val(txtNumber.Text))
       If txtMinus.Text = "-" Then number = 0 - number
    

    How to create an "Decimal" number only textbox that allows negative numbers
    Picture of initial project Your form will now look like this
    Picture of completed project
       Private Sub txtNumber_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtNumber.KeyPress
            If e.KeyChar = Chr(8) Then Exit Sub
            If Not "1234567890+-.".Contains(e.KeyChar) Then e.Handled = True
            If e.KeyChar = "+" Then
                txtMinus.Text = "+"
                e.Handled = True
            End If
            If e.KeyChar = "-" Then
                txtMinus.Text = "-"
                e.Handled = True
            End If
            If ((e.KeyChar = ".") And (txtNumber.Text.Contains("."))) Then e.Handled = True
        End Sub
    
    How to read the value in txtNumber
       Dim number As Double
       number = Val(txtNumber.Text)
       If txtMinus.Text = "-" Then number = 0 - number
    
    DigitalDan.uk is part of the DigitalDan.co.uk group