473,325 Members | 2,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Checking for Characters

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.
Nov 21 '05 #1
5 1267
Playa,

"Playa" <Pl***@discussions.microsoft.com> schrieb:
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
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #2

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

Nov 21 '05 #3
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
Nov 21 '05 #4
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

Nov 21 '05 #5
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.


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!
Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Xam | last post by:
Hello everybody Do you know of a javascript routine that can warn if there are any pre-defined invalid chars in the filename of an INPUT file box before it is submitted with the submit button. ...
1
by: lkrubner | last post by:
this is a function that someone has up on www.php.net: function seemsUTF8($Str) { // bmorel at ssi dot fr //17-Feb-2004 01:22 //Here is an improved version of that function, compatible with...
1
by: Oenone | last post by:
Is there a nice .NET framework function that will allow me to easily check if a string contains only alphanumeric characters? Or is the only way to loop through the characters, checking each one...
26
by: libsfan01 | last post by:
Hi all! Can anyone show me how to check and email field on a form for the existence of these two characters. Kind regards Marc
7
by: JJ | last post by:
To validate a password as the user is registering I want to use a regular expressio validator. I got this one from the Microsoft web site for validating a password of at least 7 characters, with...
42
by: =?Utf-8?B?UGxheWE=?= | last post by:
I have an if statement that isn't working correctly and I was wondering how I check for a blank string. My Code Example if me.fieldname(arrayIndex) = "" then ----- end if When I do this and...
6
by: Jeff | last post by:
Could someone tell me the easiest way to check a string of variable length to see if it consists of all blank characters? ....or perhaps more generally, to see if all of the characters are the...
125
by: jacob navia | last post by:
We hear very often in this discussion group that bounds checking, or safety tests are too expensive to be used in C. Several researchers of UCSD have published an interesting paper about this...
11
by: Bryan Crouse | last post by:
I am looking a way to do error checking on a string at compile time, and if the string isn't the correct length have then have the compiler throw an error. I am working an embedded software that...
2
by: jou00jou | last post by:
Hi, I have trouble using sscanf and fgets to check for overflow. I will post the assignment specification so I could help whoever would kindly like to offer his/her help. ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.