Whole number

dezreena

Member
Joined
Mar 29, 2007
Messages
20
Programming Experience
Beginner
hi guys,

I have a quick question,how can i prove my text box is only for whole number?i dont allow the user to key in decimal place in the text box.what code should i use?

VB.NET:
Private sub textbox1_changed()
 
If Not Int(Me.textbox1.Text) then
Msgbox("Please key in only whole number")
End If

End sub

This code is not functioning in a correct way.pls help.thx!
 
VB.NET:
        Dim i As Integer
        If Not Integer.TryParse(TextBox1.Text, i) Then
            MsgBox("enter whole number")
        End If
It would perhaps also be better to use the NumericUpDown control.
 
KeyPress event can limit user's input.

Place this code in the textbox's KeyPress event. It will only permit the user to enter digits between 0-9.

If Not Char.IsDigit Then e.Handled = True

You will also need to allow use of the Backspace key, so add this statement after the above one:

If e.KeyChar = Chr(8) Then e.Handled = False


You may also want the user to press the Enter key when done, and move to the next control:

If e.KeyChar = Chr(13) Then TextBox2.Focus()
 
Back
Top