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:
I would suggest doing this on the OnLeave event rather than the TextChanged event. This way you won't have the issue that you are having. If you want to continue using the TextChanged event, you might want to check if the text is a number before you do your range validation.
 
Thanx for your post but im not sure what you mean by Onleave even and the Textchanged event. I dont know whats the difference between them. After many attempts i tried the following code which almost did the trick :

VB.NET:
        Dim intTemp As Integer
        If Not Integer.TryParse(pa.Text, intTemp) Then
            MsgBox("Warning: You entered text instead of a number")
        End If

        If intTemp > 25 Then
            pa.Text = 25
            MsgBox("Pressure angle cannot exceed 25!", vbExclamation, "Error!")
        ElseIf intTemp < 0 Then
            pa.Text = 0
            MsgBox("Pressure angle cannot be less than zero!", vbExclamation, "Error!")
        End If

The only problem is that i get a warning message when i hit the minus sign - or even when i use backspace i get the warning mesage. is there anyway that i can make exceptions for both the minus sign and the backspace ?
 
From your code (in the screenshot) I can see that you are doing your validation in the TextChanged event of the textbox. This will fire (and thus run your validation code) everytime the text changes in the textbox. It seems that you would not want that happening. Instead, move your original code to the Leave event and you shouldn't get this issue (unless the user enters anything other than the negative sign or a number).
 
No, I don't. It seems you should read up a little more about software development (not being condescending, you should probably just grab a book on VB.Net development, it would probably help).

Every control has a list of events that you can subscribe to. You are doing your validation code in the TextChanged event handler (which is called when the text changes in the text box). If you go to the designer, select the text box and then go to properties and click on the lightening bolt you will see the list of events that you can subscribe to. One of these events is the Leave event, this will be fired whenever the user "leaves" the text box (the text box loses focus). This is probably where you want to do your validation in order to avoid the errors that you are receiving.

BTW, are you writing a spur gear checking program? Just wondering because I work at a company that does gear checking and I am on the team that is developing the gear checking software.
 
If you're looking for a number >=1 and <= 25, why don't you use a NumericUpDown control instead of a textbox?
 
No, I don't. It seems you should read up a little more about software development (not being condescending, you should probably just grab a book on VB.Net development, it would probably help).

Every control has a list of events that you can subscribe to. You are doing your validation code in the TextChanged event handler (which is called when the text changes in the text box). If you go to the designer, select the text box and then go to properties and click on the lightening bolt you will see the list of events that you can subscribe to. One of these events is the Leave event, this will be fired whenever the user "leaves" the text box (the text box loses focus). This is probably where you want to do your validation in order to avoid the errors that you are receiving.

BTW, are you writing a spur gear checking program? Just wondering because I work at a company that does gear checking and I am on the team that is developing the gear checking software.


Thanks for clarifying things. The software is to manufacture spur gears with the help of CNC machines. I have the software built on excel from scratch now i want to rebuild it using VB. Anyways :s

Previously it was like this

generalby3.jpg


Now its like this :huh:

emptyby1.jpg
 
I would just do this

VB.NET:
if isnumeric(pa.text) then
       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
else
msgbox("Can not use non numeric charectors")
end
 
Thanks but ive ued the following code



VB.NET:
  Private Sub pa_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles pa.LostFocus
        If Not String.IsNullOrEmpty(pa.Text) AndAlso Not pa.Text = "" Then
            'This checks for blanks in the text boxDim intPA as Integer = Convert.ToInt32(pa.Text)
            Dim intPA As Integer = Convert.ToInt32(pa.Text)
            If intPA > 25 Then
                pa.Text = 25
                MessageBox.Show("Pressure angle cannot exceed 25 degrees!", "Pressure angle", MessageBoxButtons.OK, MessageBoxIcon.Information)
                pa.Focus()
            ElseIf intPA < 0 Then
                pa.Text = 0
                MessageBox.Show("Pressure angle cannot be less than 0 degrees!", "Pressure angle", MessageBoxButtons.OK, MessageBoxIcon.Information)
                pa.Focus()
            End If
        End If

i entered the following code i still want to modify the code so that when the calculate button and text is entered i get a msgbox saying
if text was entered
msgbox "Text was entered!"
pa.text = Clear
end if

how am i able to do so ?




a1yl2.jpg


a2pu7.jpg



a3ef2.jpg


a4sv0.jpg




a5gf4.jpg


a6ky6.jpg
 
Last edited:
I found the solution and it works like charm

VB.NET:
Private Sub pa_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles pa.LostFocus
 
Dim intPA As Integer
If Not String.IsNullOrEmpty(pa.Text) AndAlso Not pa.Text = "" Then
If Not Integer.TryParse(pa.Text, intPA) Then
'Text was entered
MessageBox.Show("Text was entered!", "Pressure angle", MessageBoxButtons.OK, MessageBoxIcon.Error)
pa.Text = ""
Else
'This checks for blanks in the text boxDim
If intPA > 25 Then
pa.Text = 25
MessageBox.Show("Pressure angle cannot exceed 25 degrees!", "Pressure angle", MessageBoxButtons.OK, MessageBoxIcon.Information)
pa.Focus()
ElseIf intPA < 0 Then
pa.Text = 0
MessageBox.Show("Pressure angle cannot be less than 0 degrees!", "Pressure angle", MessageBoxButtons.OK, MessageBoxIcon.Information)
pa.Focus()
End If
End If
End If
End Sub
 
If im to modify the above code and let it accept decimals what should i modify i tried the following but no luck:

VB.NET:
  Dim intPA As Double = 0.0
        intPA = Convert.ToDouble(pa.Text)
        If Not String.IsNullOrEmpty(pa.Text) AndAlso Not pa.Text = "" Then

            If Not Integer.TryParse(pa.Text, intPA) Then
                'Text was entered
                MessageBox.Show("Text was entered!", "Pressure angle", MessageBoxButtons.OK, MessageBoxIcon.Error)
                pa.Text = ""
            Else
                'This checks for blanks in the text boxDim
                If intPA > 25 Then
                    pa.Text = 25
                    MessageBox.Show("Pressure angle cannot exceed 25 degrees!", "Pressure angle", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    pa.Focus()
                ElseIf intPA < 0 Then
                    pa.Text = 0
                    MessageBox.Show("Pressure angle cannot be less than 0 degrees!", "Pressure angle", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    pa.Focus()
                End If
            End If
        End If

    End Sub


http://img525.imageshack.us/img525/3500/decimallp1.jpg
 
Back
Top