How can i limit a txt value between 1->25 and return an error msg if the value was...

Beginner

Well-known member
Joined
Mar 12, 2008
Messages
114
Programming Experience
Beginner
How can i limit a txt value between 1->25 and return an error msg if the value was...

VB.NET:
        Dim PresureA As Integer
        If (pa.Text > 25) Then
            pa.Text = 25
            PresureA = MsgBox("Pressure angle cannot exceed 25!", vbExclamation, "Error!")
        End If
        If (pa.Text < 0) Then
            pa.Text = 0
            PresureA = MsgBox("Pressure angle cannot be less than zero!", vbExclamation, "Error!")
        End If

The only problem now is when i enter a negative number the program fails.

minusgt4.jpg


Edit:Under these circumstances, if the user entered the number -27,
the control wouldn't accept it because the beginning dash isn't
a number
 
Last edited:
If you're looking for a number >=1 and <= 25, why don't you use a NumericUpDown control instead of a textbox?

Amen to this


Textboxes are for gathering TEXT.. NOT NUMBERS.
There is a huge difference between these two things:

"25"
25

One is a string of text containing numerical characters. One is a number. If the OP isnt sure of the difference, he needs to study more and code less.

I cannot approve of Juggalo's suggestion enough:
To ask the user for a number, use a NumericUpDown control! Its a number based control, has min and max values and can have a varying number of decimal places. The cursor keys alter the value in a logical way. The presence of the spinner lets the user know it's numerical only.. The benefits list just goes on..

Beginner, I dont mean to pour water on your parade of efforts, but the time you've put into abusing a textbox to accept numbers only is nearly a complete waste and you've succeeded in making a very annoying user interface. Every time the user types something that is not a number, they get an interruptive message box that they must respond to.. The slightest typo and the computer makes a BONG error noise, and gives them an unhelpful and somewhat patronising error message that does little more than state the obvious (and irritate them).
Yuck


To solve your problem, do the following:
Open the form designer
click on the textbox youre messing with
press the delete key
open the Toolbox
find the NumericUpDown control
double click it to put one on the form
drag it to location

In the properties window of the numericupdown:
Set the Min and Max values to sensible limits
Set the number of decimal places you want
Set the increment, so that when the user presses the arrow keys, the box increments by a sensible amount

Change every reference in your code from the textbox.Text you had, to:

numericupdownName.Value


This process will take less time than it did to compose your first post ;)

-

And definitely read up on the difference between a String and a Number. You should consider switching on Option Strict and Option Explicit to ahve the compiler help you identify all the points in your cod ethat youre causing an implicit conversion between data types (like myTextbox.Text > 25)
 
From those pics it looks like you have a lot of numerical entry.. change them ALL for NumericUpDown :)
 
Previously it was like this

generalby3.jpg


Now its like this :huh:

emptyby1.jpg

PS the reason for this is found in the Solution explorer on the right hand side of the screenshots..

Look on the first shot, the open file is a Form, as evidenced by the grey rectangular icon. The second screenshot shows that the file has become just a normal VB document as evidenced by the page-of-paper-with-a-vb-logo icon

Probably the form's resx file has been deleted and it wont now be openable with the visual designer

pps: DONT put spaces in file names.. ("Spur Gear.vb")
 
Thanks for the tips and why dont u advice me to use spaces. In what way will it affect the program performance ?

Anyways 2 questions i tried the NumericUpDown now the default is 1 which im ok with but is there anyworkaround to set it to blank ?

clearallib9.jpg


Second thing: i was some invisible mathematical calculation to take place so as soon as the value in the NumericUpDown changes the textbox onto the right hand side executes the answer straight away without the need for waiting for the cursor to move to the next input. My code is working fine but where do you suggest i input this ?


Dim N As Integer
Integer.TryParse(gearteeth1.Text, N)
resultn.Text = ((360 / N) * Math.PI) / 180


previously when i had text boxes i put the code as shown in the picture and changes take place immediately. Where shall i put the same code if im dealing with NumericUpDown.


1and2bm2.jpg
 
Thanks for the tips and why dont u advice me to use spaces. In what way will it affect the program performance ?

Anyways 2 questions i tried the NumericUpDown now the default is 1 which im ok with but is there anyworkaround to set it to blank ?

Are all the values needed for the calculation? If yes, then why do you care about seeting them blank. Set sensible defaults instead. If some may be left blank, then provide a radiobutton group that will selectively enable and disable the relevant NUDs depending on which calculation is being performed

Second thing: i was some invisible mathematical calculation to take place so as soon as the value in the NumericUpDown changes the textbox onto the right hand side executes the answer straight away without the need for waiting for the cursor to move to the next input. My code is working fine but where do you suggest i input this ?

Erm.. the ValueChanged event handler?

VB.NET:
        Dim N As Integer
        Integer.TryParse(gearteeth1.Text, N)
        resultn.Text = ((360 / N) * Math.PI) / 180
Give variables meaningful names
Use camelCaseThatLooksLikeThisAndStartsWithLowerCase for variable names
Integer is a whole number with no decimal component.. Does that suit your precision requirements? (I doubt it unless youre wroking with very small units, knowing what little I do of CNC)
TryParse returns a boolean for a reason, not that you'll need it now that you ought to be using NUDs and .Value (which is already a number - see previous note about reading up on the difference between text and numbers)

previously when i had text boxes i put the code as shown in the picture and changes take place immediately. Where shall i put the same code if im dealing with NumericUpDown.
Well, before you were using the .Text property and the event you were hooking was TextChanged. Somewhat unsurprisingly, now youre using the .Value property, you probably want to be notified every time the Value has Changed.. which would be the relevant ValueChanged event
 
Erm.. the ValueChanged event handler?

Why do i get different values ?

error2tc5.jpg


error1th4.jpg


My code is :

VB.NET:
    Private Sub gearteeth1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gearteeth1.ValueChanged

        Dim N As Integer
        Integer.TryParse(gearteeth1.Text, N)
        resultn.Text = ((360 / N) * Math.PI) / 180
    End Sub

I renamed NUD to geerteeth1
resultn.text is the text box on the right.


Integer is a whole number with no decimal component.. Does that suit your precision requirements? (I doubt it unless youre wroking with very small units, knowing what little I do of CNC)

I want the value to be decimalized. May you tell me how ?


Thanks in advance.
 
I get the feeling that youre not really listening to what i'm saying, or thinking about the code you have written. Good luck with your project

cjard said:
Textboxes are for gathering TEXT.. NOT NUMBERS.
There is a huge difference between these two things:

"25"
25

One is a string of text containing numerical characters. One is a number. If [you arent] sure of the difference, [you] need to study more and code less


cjard said:
Change every reference in your code from the textbox.Text you had, to:

numericupdownName.Value
 
Good luck to you


VB.NET:
Originally Posted by cjard 
Textboxes are for gathering TEXT.. NOT NUMBERS.
There is a huge difference between these two things:

"25"
25

One is a string of text containing numerical characters. One is a number. If [you arent] sure of the difference, [you] need to study more and code less

The input is the NUD what shall i do for output another NUD ?! i want to make a greyed out text that just gives output! but im stil thinking how can that be done.


VB.NET:
Originally Posted by cjard 
Change every reference in your code from the textbox.Text you had, to:

numericupdownName.Value

i dont know how to do that i messed around a lot !
 
It seems this is your first time venturing into software development, you would really benefit WAY more from picking up a VB.Net 2005 development book and reading through that, doing the examples, than you would stumbling through this project. You will learn way more from that then you will from trial and error of building this software. Any questions you have we will be more than happy to answer, but you are kind of stumbling on the very basics here.
 
im stubmling a lot. I want this project to be done coz im getting graded on this. Back to the subject :D

Finally got it to work:


VB.NET:
Private Sub gearteeth1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gearteeth1.ValueChanged

If gearteeth1.Value > 0 Then

resultn.Text = (((360 / gearteeth1.Value) * Math.PI) / 180).ToString

Else

resultn.Text = "Must have some teeth"

End If

End Sub

if i want to disable keyboard user input and force the user to use the up and down arrows what property should i change ?
 
I am not sure I agree with using a NUD. This is fine on values between 1-25, but if something changes to where the values are 1 and 2000, then it is not a great solution.

Now, back to my original post. .Net has a built in function called "isnumeric" This checks the data to verify it is a number, and is a better way to validate a textbox when someone enters data.


#2, a textbox will display something anyway you want using the format property. But the type is decimal that you will need

Dim d As Decimal
If IsNumeric(mytextbox.text) Then
d.Parse(mytextbox.text)
Else
'not numeric
End If
 
I am not sure I agree with using a NUD. This is fine on values between 1-25, but if something changes to where the values are 1 and 2000, then it is not a great solution.

Now, back to my original post. .Net has a built in function called "isnumeric" This checks the data to verify it is a number, and is a better way to validate a textbox when someone enters data.


#2, a textbox will display something anyway you want using the format property. But the type is decimal that you will need

Dim d As Decimal
If IsNumeric(mytextbox.text) Then
d.Parse(mytextbox.text)
Else
'not numeric
End If

Thanx but where exactly should this be entered under the NUD ? same as previous ?
 
I am not sure I agree with using a NUD. This is fine on values between 1-25, but if something changes to where the values are 1 and 2000, then it is not a great solution.

Now, back to my original post. .Net has a built in function called "isnumeric" This checks the data to verify it is a number, and is a better way to validate a textbox when someone enters data.


#2, a textbox will display something anyway you want using the format property. But the type is decimal that you will need

Dim d As Decimal
If IsNumeric(mytextbox.text) Then
d.Parse(mytextbox.text)
Else
'not numeric
End If
NUD's are good for a number to 99 million or something like that. For a number that large, the user isn't going to want to click the up arrow on the spinner to reach that number, but NUD's allow the user to type in a number from the keyboard just like a textbox. This is why NUD's are much better than forcing a textbox to do things outside it's scope of tasks
 
Thanks Buddy and i got the decimal thingy working with no VB code. Playing around with the properties ,, this might sound odd but it worked LOL
the clear command


i did gearteeth1.text="" it did the trick for both NUD so
YESSSSSSS IT CLEARS LOOOOOL.
 
To go even further, with NUD's you don't have to use the Text property at all. To "clear" the NUD set the Value property equal to zero (0D) and it'll reset, even if the minimum has to be higher than 0 (if you set it to zero, it'll actually set it to whatever the minimum allowed is)
 
Back
Top