I need to be able to limit the text entered into a text box, I need to make it so that only whole numbers can be entered and not decimal places. I have already limited it to just Numbers but need help with this. I think checking a box to see if a fullstop is in it would be a good way but i dont know the command for searching a string.

to limit text box set its property "MaxLength" to number you want to enter like to 3 or 6.
to check decimal put condition
somthing like this...
if textbox1.text=Asc(.) then
msgbox("Please dont enter Decimal Point, Thanks")
endif

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Dim dt As Integer = Asc(Me.TextBox1.Text)
If TextBox1.Text = Chr(46) Then
MsgBox("You pressed dot")
End If
End Sub


this is one way you can check whether the dot is pressed or not

try this following code, this code only allowed number entered on textbox :

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If (Microsoft.VisualBasic.Asc(e.KeyChar) < 48) _
               Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 57) Then
            e.Handled = True
        End If
        If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
            e.Handled = False
        End If
end sub

Hi
Try the following code So that you can enter only the numbers not even any special characters.

try it in the textbox keypress event

Private Sub textbox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tsum.KeyPress
If Asc(e.KeyChar) = 8 Then
Exit Sub
End If
If Asc(e.KeyChar) = 44 Then
e.Handled = True
End If

If Asc(e.KeyChar) < 48 Then
e.Handled = True
ElseIf Asc(e.KeyChar) > 57 Then
e.Handled = True
End If
End Sub

I need to be able to limit the text entered into a text box, I need to make it so that only whole numbers can be entered and not decimal places. I have already limited it to just Numbers but need help with this. I think checking a box to see if a fullstop is in it would be a good way but i dont know the command for searching a string.

You could also use the IsNumeric to insure all numbers and then check for the (.) as the other answer stated.

The MaxLength function will limit the number of characters.

You could also use code to check on keydown event if the character being enter is a number or period. If the period is captured, trigger a messagebox event and remove the period and reset the focus at the end of the numeric string.

just try this if will help you find if you type dot in the textbox
If TextBox1.Text.Contains(".") Then
MsgBox("true")
Else
MsgBox("true")
End If

Hi
Try the following code So that you can enter only the numbers not even any special characters.

try it in the textbox keypress event

Private Sub textbox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tsum.KeyPress
If Asc(e.KeyChar) = 8 Then
Exit Sub
End If
If Asc(e.KeyChar) = 44 Then
e.Handled = True
End If

If Asc(e.KeyChar) < 48 Then
e.Handled = True
ElseIf Asc(e.KeyChar) > 57 Then
e.Handled = True
End If
End Sub

If IsNumeric(TextBox1.Text) And TextBox1.Text.Contains(".") Then
MsgBox("True for .")
Else
MsgBox("False for .")
End If

you can try the code below.. i just copied the source from this link: http://www.philtrend.org/vb-net-simple-restricting-textbox-free-code/

Dim iText As String = TextBox1.Text
Dim Letter As String
Dim SelectionIndex As Integer = TextBox1.SelectionStart
Dim Change As Integer
Dim charAllowed As String = ” abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890″

For x As Integer = 0 To TextBox1.Text.Length – 1
Letter = TextBox1.Text.Substring(x, 1)
If charAllowed.Contains(Letter) = False Then
iText = iText.Replace(Letter, String.Empty)
Change = 1
End If
Next

TextBox1.Text = iText
TextBox1.Select(SelectionIndex – Change, 0)

just wanna share coz this one is also my problem before ^_^

Member Avatar for §AE§

An easy why to do this is to prevent it from happening. To do this easily here is the code:

Public Class Form1
    Dim PreviousText As String
    Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
        If TextBox1.Text.Contains(".") Or (Not IsNumeric(TextBox1.Text) And TextBox1.Text.Trim <> "") Then TextBox1.Text = PreviousText
        PreviousText = TextBox1.Text
    End Sub
End Class

If you need to notify them try an error reporter or a message box

A better method is to use the KeyDown event to suppress unwanted keys

Private Sub TextBox1_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown

    Select Case e.KeyValue
        Case 50 To 59           'allow digits
        Case 8, 37, 39, 46      'allow bs, left, right, del
        Case Else
            e.SuppressKeyPress = True
    End Select

End Sub

@ Jim
Now you are starting answering 6 years old posts. Trolls on Dani? There wasjust one answer to a question from 10 years ago.

I know but I couldn't just leave a bad answer as the last post in a thread - even a ten year old one.

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.