logo VB.Net - Numeric Textbox
Guide Contents
DigitalDan Home Page

Note Visual Basic has a special MaskedTextbox control. This control is an extended version of the normal Textbox and uses a nask to regulate what can be typed. The default VB.Net settings do not show the MaskedTextBox in the Toolbox. On my vesion of dotNet, the following steps added teh control to my toolbox, you may have to browse the internet for advice relating to other versions.
 
Note This page only uses normal textboxes/buttons. The commennts about maskestextbox are only intended to indicate that their may be an alternative solution to your coding problem.

 
Textbox that only accepts positive integers
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Chr(8) Then Exit Sub ' 1
If Not "0123456789".Contains(e.KeyChar) Then
e.Handled = True ' 2
End If
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Dim oldText As String = TextBox1.Text
Dim regex As New System.Text.RegularExpressions.Regex("[^0-9]")
Dim newText As String = regex.Replace(oldText, "") ' 3
If oldText <> newtext Then ' 4
TextBox1.Text = newText
End If
End Sub
Private Function Read_TextBox1() As Integer
Dim ret As Integer
If Not Integer.TryParse(TextBox1.Text, ret) Then Return 0 ' 5
Return ret
End Function


 
Textbox anly accepts +ve number (allows decimal point)
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Chr(8) Then Exit Sub
If Not "0123456789.".Contains(e.KeyChar) Then
e.Handled = True
End If
If e.KeyChar = "."c AndAlso TextBox1.Text.Contains("."c) Then
e.Handled = True
End If
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Dim oldText As String = TextBox1.Text
Dim regex As New System.Text.RegularExpressions.Regex("[^0-9.]")
' this version does not alloiw exponential numbers
Dim newText As String = regex.Replace(oldText, "")
' ensure maximum of one decimal point
Dim parts() As String = newText.Split("."c)
If parts.Length > 2 Then
newText = parts(0) & "."c & parts(1)
End If
If oldText <> newText Then
TextBox1.Text = newText
MsgBox("changed")
End If
End Sub
Private Function Read_TextBox1() As Double
Dim ret As Double
If Not Double.TryParse(TextBox1.Text, ret) Then Return 0
Return ret
End Function

 
Textbox - Integer Only, Accepts negative numbers
The user needs to be able to enter - or + to indicate whether number is negative or positive. However, we do not want - or plus appearing in teh middle of a number - that would be confusing. One silution is to move the - or + outside of the Textbox. If we place it in a button., next to the textbox, user can rapidly switch - or + by clicking the button.
textbox and button on form
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Chr(8) Then Exit Sub
If Not "0123456789+-".Contains(e.KeyChar) Then
e.Handled = True
End If
If e.KeyChar = "+"c Then
Button1.Text = "+"
e.Handled = True
End If
If e.KeyChar = "-"c Then
Button1.Text = "-"
e.Handled = True
End If
End Sub Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Dim oldText As String = TextBox1.Text
Dim regex As New System.Text.RegularExpressions.Regex("[^0-9+-]")
' this version does not allow exponential numbers
Dim newText As String = regex.Replace(oldText, String.Empty)
For Each c As Char In newText
If c = "+"c Then Button1.Text = "+"
If c = "-"c Then Button1.Text = "-"
Next
newText = newText.Replace("+"c, String.Empty).Replace("-"c, String.Empty)
If oldText <> newText Then
TextBox1.Text = newText
MsgBox("changed")
End If
End Sub
Private Function Read_TextBox1() As Integer
Dim ret As Integer
Dim num As String = TextBox1.Text.Replace("+"c, String.Empty)
num = num.Replace("-", String.Empty)
If Button1.Text = "-" Then num = "-" & num
If Not Integer.TryParse(num, ret) Then Return 0
Return ret
End Function

 
Numeric Textbox - Accepts negative and decimal point
The user needs to be able to enter - or + to indicate whether number is negative or positive. However, we do not want - or plus appearing in teh middle of a number - that would be confusing. One silution is to move the - or + outside of the Textbox. If we place it in a button., next to the textbox, user can rapidly switch - or + by clicking the button.
textbox and button on form
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Chr(8) Then Exit Sub
If Not "0123456789+-.".Contains(e.KeyChar) Then
e.Handled = True
End If
If e.KeyChar = "."c AndAlso TextBox1.Text.Contains("."c) Then
e.Handled = True
End If
If e.KeyChar = "+"c Then
Button1.Text = "+"
e.Handled = True
End If
If e.KeyChar = "-"c Then
Button1.Text = "-"
e.Handled = True
End If
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Dim oldText As String = TextBox1.Text
Dim regex As New System.Text.RegularExpressions.Regex("[^0-9+.-]")
' this version does not allow exponential numbers
Dim newText As String = regex.Replace(oldText, String.Empty)
For Each c As Char In newText
If c = "+"c Then Button1.Text = "+"
If c = "-"c Then Button1.Text = "-"
Next
newText = newText.Replace("+"c, String.Empty).Replace("-"c, String.Empty)
Dim parts() As String = newText.Split("."c)
If parts.Length > 2 Then
newText = parts(0) & "." & parts(1)
End If
If oldText <> newText Then
TextBox1.Text = newText
MsgBox("changed")
End If
End Sub
Private Function Read_TextBox1() As Integer
Dim ret As Integer
Dim num As String = TextBox1.Text.Replace("+"c, String.Empty)
num = num.Replace("-", String.Empty)
If Button1.Text = "-" Then num = "-" & num
If Not Integer.TryParse(num, ret) Then Return 0
Return ret
End Function

DigitalDan.co.uk ... Hits = 108