Only Allow Integers In A TextBox

Only Allow Integers In A TextBox But If The User Enters A Letter It Wi

Page 1 of 1

3 Replies - 5991 Views - Last Post: 19 April 2007 - 09:18 AM Rate Topic: -----

#1 dominatingpcs  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 01-April 07

Only Allow Integers In A TextBox

Post icon  Posted 17 April 2007 - 04:58 PM

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

Is This A Good Question/Topic? 1

Replies To: Only Allow Integers In A TextBox

#2 m2s87  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 390
  • Joined: 28-November 06

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. :D 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 :blink:, 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 post has been edited by m2s87: 17 April 2007 - 05:39 PM

Was This Post Helpful? 0
  • +
  • -

#3 orangeslide8  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 203
  • Joined: 29-December 06

Re: Only Allow Integers In A TextBox

Posted 19 April 2007 - 06:32 AM

the two ways i would do it is either use
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
Was This Post Helpful? 0
  • +
  • -

#4 Nightshade  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-February 07

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
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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1