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
This code demonstrates how to read our txtNumber textboxtxtNumber.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
How to create an "Integer" number only textbox that allows negative numbers
How to create an "Decimal" number only textbox that allows negative numbers
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

- Create a new form in VB.net
- Rename your form as "frmTest"
- Add a normal textbox to your new frmTest
- Rename your textbox as "txtNumber"
- Find the MaximumLength property of your text box and change it to 5
- Double click on the form to access the screen to type program code
- Add this code, I will explain it later.
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
- Handles txtNumber.KeyPress asks computer to run the Sub whenever the textbox is in use and the keyboard is used
- e.keyChar is a special variable, this will contain the letter/number that the user is attempting to type
- Chr(8) is a special code used by the delete key, we don't want top block this keyboard key
- "1234567890".contains(e.KeyChar) asks computer to decide whether the keyboard key pressed is a digit (0-9)
- Not "12345..... will detect any key press that is Not a digit (A-Z, symbols, a-z etc.)
- e.Handled = True tells computer to ignore the key just pressed (do not add it to textbox)
This code demonstrates how to read our txtNumber textbox
Dim number As Integer number = CInt(Val(txtNumber.Text))
How to create a "Decimal" number only textbox

- Create a new form in VB.net
- Rename your form as "frmTest"
- Add a normal textbox to your new frmTest
- Rename your textbox as "txtNumber"
- Find the MaximumLength property of your text box and change it to 8
- Double click on the form to access the screen to type program code
- Add this code, it is based on previous example
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 SubThis 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

- Create a new form in VB.net
- Rename your form as "frmTest"
- Add a normal textbox to your new frmTest
- Rename your textbox as "txtMinus"
- Adjust the size and position of this textbox to make it look like the one on the left of the picture
- Change the txtMinus Text property to +
- Change the txtMinus BorderStyle property to None
- Change the txtMinus ReadOnly property to True
- Add a normal textbox to your new frmTest
- Rename your textbox as "txtNumber"
- Adjust the size and position of this textbox to make it look like the one on the right of the picture
- Change the txtNumber MaximumLength proprty to 8

- Double click on the form to access the screen to type program code
- Add this code, it is based on the first example
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
- I used a separate textbox for the plus/minus sign to simplify the code.
- If I wanted the +/- in same textbox I would have to consider where to resume typing after entering the sign
- I must now check for plus/minus when readingthe value from txtNumber
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

- Create a new form in VB.net
- Rename your form as "frmTest"
- Add a normal textbox to your new frmTest
- Rename your textbox as "txtMinus"
- Adjust the size and position of this textbox to make it look like the one on the left of the picture
- Change the txtMinus Text property to +
- Change the txtMinus BorderStyle property to None
- Change the txtMinus ReadOnly property to True
- Add a normal textbox to your new frmTest
- Rename your textbox as "txtNumber"
- Adjust the size and position of this textbox to make it look like the one on the right of the picture
- Change the txtNumber MaximumLength proprty to 8

- Double click on the form to access the screen to type program code
- Add this code, it is based on the first example
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 SubHow 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