On Sat, 19 Feb 2005 07:21:03 -0800, "Playa"
<Pl***@discussions.microsoft.com> wrote:
I have a textbox and I only want the user to enter numbers. When the user
clicks the calculate event I want to check to make sure only numbers are
entered and if there is characters i want to display a message box. New to
this language and have no idea how to code that. Can anyone help?
Thanks in advance.
This may help. TB_Input is the text box in question.
Private Sub Tb_Input_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles Tb_Input.KeyPress
Dim AsciiVal As Integer
AsciiVal = Asc(e.KeyChar)
Select Case AsciiVal
'Allow keys 1 to 9 and Backspace
Case Asc("0") To Asc("9"), Asc(ControlChars.Back)
Lb_Msg_1.Text = ""
Lb_Msg_2.Text = ""
e.Handled = False
Case Asc(".") 'Allow only 1 decimal point
If InStr(Tb_Input.Text, ".") = 0 Then
Lb_Msg_1.Text = ""
Lb_Msg_2.Text = ""
e.Handled = False
Else
Lb_Msg_1.Text = "Only one decimal point is
allowed"
Lb_Msg_2.Text = "in each entry!"
e.Handled = True
End If
Case Asc("-") 'Allow only 1 "-" sign and only in position
1
If InStr(Tb_Input.Text, "-") <> 0 Or
Tb_Input.SelectionStart <> 0 Then
Lb_Msg_1.Text = "Only one - (minus) sign is
allowed, and"
Lb_Msg_2.Text = "must be placed before the
number!"
e.Handled = True
Else
Lb_Msg_1.Text = ""
Lb_Msg_2.Text = ""
e.Handled = False
End If
Case Asc(ControlChars.Cr) 'Act on Return Key
Btn_Submit.PerformClick()
e.Handled = False
Case Else 'Discard all other Keys
Lb_Msg_1.Text = "Only numeric values are allowed!"
Lb_Msg_2.Text = ""
e.Handled = True
End Select
End Sub