Hello Everyone,
I am working on a project that allows a user to enter text into a textbox. The thing is that the user can only enter integers and if they enter a letter it will add the number of times the user incorrectly entered an error into a label. I am having trouble with the letter part.
:This Code Works Which Will Only Allow Integers In The Text Box:
Dim Number As Double
'Dim adderror As Integer
Try
Number = Double.Parse(Me.TextBox1.Text)
Me.TextBox1.Focus()
Number = Decimal.Parse(Me.TextBox1.Text)
Catch eFormat As SystemException
MessageBox.Show("Please Enter A Number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
TextBox1.BackColor = Color.Red
' adderror = adderror + 1
Exit Sub
'Number = Convert.ToString(adderror)
End Try
Me.TextBox1.Focus()
::This Code Doesnt Work Where If The User Enters A Letter a through z It Wont Add The Number Of Errors To The Label:::
Static Wrong As Integer
If UserControl11.Text = "I Don't Know What Code To Put Here For Letters A Through Z I have tried char but nothing" Then
Wrong = Wrong + 1
End If
'Label1.Text = Convert.ToString(Wrong) << This takes the number wrong and puts it in a label .
-Can someone please help me
Only Allow Integers In A TextBoxOnly Allow Integers In A TextBox But If The User Enters A Letter It Wi
Page 1 of 1
3 Replies - 5080 Views - Last Post: 19 April 2007 - 09:18 AM
Replies To: Only Allow Integers In A TextBox
#2
Re: Only Allow Integers In A TextBox
Posted 17 April 2007 - 05:38 PM
Well dominatingpcs,
this is usaly 1 part of a homework, and you have to solve it yourself. But i would adwise you with following:
It is a 'real' problem to solve this, beacuse there is so many ways to do it.
Just pick 1 you like the most:
1. Read only spesific keys of keyboard input
2. Irritate/anoy user if (s)he makes a mistake
3. Use the data you get, and get best of it
NB: You can replace Double.Parse(Me.TextBox1.Text) with Double.Parse(Val(Me.TextBox1.Text)) to ignore the error
Probably the meanest way (2) is to teach the user only to insert correct values
, this is where you can get creative.
But, i guess teachers would be most happy, if you would:
Use (prefarebly) labels, with a click event, that fires up new form numbrd. The numbrd should read all the key inputs needed to preform the action & have buttons to type the number in with a mouse.
this is usaly 1 part of a homework, and you have to solve it yourself. But i would adwise you with following:
It is a 'real' problem to solve this, beacuse there is so many ways to do it.
1. Read only spesific keys of keyboard input
2. Irritate/anoy user if (s)he makes a mistake
3. Use the data you get, and get best of it
NB: You can replace Double.Parse(Me.TextBox1.Text) with Double.Parse(Val(Me.TextBox1.Text)) to ignore the error
Probably the meanest way (2) is to teach the user only to insert correct values
But, i guess teachers would be most happy, if you would:
Use (prefarebly) labels, with a click event, that fires up new form numbrd. The numbrd should read all the key inputs needed to preform the action & have buttons to type the number in with a mouse.
This post has been edited by m2s87: 17 April 2007 - 05:39 PM
#3
Re: Only Allow Integers In A TextBox
Posted 19 April 2007 - 06:32 AM
the two ways i would do it is either use
or use this snippet
http://www.dreaminco.../snippet522.htm
hope that helps
john
try
' what ever you want to put then
catch ex as exeption
Msgbox("please enter an integer")
end try
or use this snippet
http://www.dreaminco.../snippet522.htm
hope that helps
john
#4
Re: Only Allow Integers In A TextBox
Posted 19 April 2007 - 09:18 AM
As this does have a homework flavor to it I will limit my answers to advice to point you in the right direction.
1. use a boolean expression to test the input
2. Look into the KeyPress Event for handling kep presses. You have some options there as well.
1. use a boolean expression to test the input
integer.tryparse(SomeTextBox.text, variableName)and use that in a selection structure to test the input. Course you will need to follow with somemethod to inform them that the input is unacceptable.
2. Look into the KeyPress Event for handling kep presses. You have some options there as well.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|