Checking for Characters

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
Playa,

Playa said:
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.

Using a messagebox is IMO not the best solution. Instead, place an
ErrorProvider component on the form and add this code:

\\\
Imports System.ComponentModel
..
..
..
Private Sub TextBox1_Validating( _
ByVal sender As Object, _
ByVal e As CancelEventArgs _
) Handles TextBox1.Validating
Dim SourceControl As TextBox = DirectCast(sender, TextBox)
Dim ErrorText As String
Try
Dim i As Integer = Integer.Parse(SourceControl.Text)
Catch
ErrorText = "Value must be an integer."
Finally
Me.ErrorProvider1.SetError( _
SourceControl, _
ErrorText _
)
End Try
End Sub
///
 
you could try this in the Textchanged event for that textbox:

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox1.TextChanged

If IsNumeric(Me.TextBox1.Text) = False Then

Me.TextBox1.Text = ""
MsgBox("only numbers, please.")

End If

End Sub
 
Playa,

A little bit roughly made in this message so check it.
Both are needed by the way.

\\\\
Private Sub textbox1_KeyUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles textbox1.KeyUp
If e.KeyValue <> 8 Then
If e.KeyValue < 48 OrElse e.KeyValue > 57 Then
MessageBox.Show("Only numbers are allowed")
TextBox1.SelectionStart = TextBox1.Text.Length - 1
TextBox1.SelectionLength = 1
End If
End If
End Sub
Private Sub TextBox1_Validating(ByVal sender _
As Object, ByVal e As System.ComponentModel.CancelEventArgs) _
Handles TextBox1.Validating
If TextBox1.Text.Length > 0 Then
For each chr as char in Textbox1.Text
If chr < "0" or chr >"9" then
MessageBox.Show("The text contains a non number")
TextBox1.Focus()
e.Cancel = True
exit For
next
End If
End Sub
////

I hope this helps a little bit?

Cor
 
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
 
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.

In my sample code posted earlier the two LB_ objects are lables used
at the foot of the window to tell users when they key an error. It
has helped stop calls like;

User : "It's not working".
Me: " explain"
USer: "I'm trying to type a name into the price field and nothing is
happening."

Need I say more, users you cant live with them and there is no job
without them!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top