none
Need help limiting the grade to be between 0 and 100 RRS feed

  • Question

  • Good morning i am coding a program and i am currently running into and issue!! I need help telling visual basic that the grade i enter into a text box can only be 0 - 100, if i enter ex."1001" the program will just crash!

    THIS IS WHAT I HAVE!

    'Name: Joshua Cruz
    'Date: 10/29/18

    Option Strict Off



    Public Class Form1
        Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
            'This event closes the form.
            Close()
        End Sub
        ''
        Private Sub btnAvg_Click(sender As Object, e As EventArgs) Handles btnAvg.Click
            'Declare each variable
            Dim grade As String
            Dim grade2 As String
            Dim grade3 As String
            'Assigning variables
            grade = txt1.Text
            grade2 = txt2.Text
            grade3 = txt3.Text
            'Simple adding and dividing to equal average
            txtAverage.Text = (grade + +grade2 + +grade3) / 3
            If txt1.Text.Length >= 4 Then
                MessageBox.Show("Enter a number between 0 and 100")
            End If

        End Sub

        Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
            'Declare variables
            Dim letter As String
            Dim score As Double

            score = txtAverage.Text
            letter = txtGrade.Text
            'Nested if which determines your average and converts into a letter grade.
            If score >= 90 And score <= 100 Then
                letter = "A"
            ElseIf score >= 80 And score < 90 Then
                letter = "B"
            ElseIf score >= 70 And score < 80 Then
                letter = "C"
            ElseIf score >= 60 And score < 70 Then
                letter = "D"
            Else
                letter = "F"
            End If
            'Wont print out into grade box
            txtGrade.Text = letter
        End Sub

        Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
            txt1.Clear()
            txt2.Clear()
            txt3.Clear()
            txtAverage.Clear()
            txtGrade.Clear()
            txt1.Focus()
        End Sub
    End Class

    Monday, October 29, 2018 4:06 PM

Answers

  • PS "genius" don't go being a grammar warrior on the internet... i appreciate the help but if your able to answer my question then you read my sentence fine right? --A holes like u are what make kids not wanna follow their dreams...

    PS

    NEXT time you right PS think before u type

    Hi

    You need to use the Quote to reply to a particular person (or use their name) otherwise we won't know who you are trying to insult.


    Regards Les, Livingston, Scotland

    • Marked as answer by Joshua Cruz 16 Wednesday, October 31, 2018 10:45 PM
    Tuesday, October 30, 2018 10:42 PM

All replies

  • Hi

    First thing I noticed. When you want to use any text (String) in a calculation, you MUST convert it to a numeric type, such as Integer, Double, Decimal etc according to your needs.

    One way is to use       Dim i as Integer = Cint(String)

    this would work for your use with one very important caveat: if you try to do that with a String that is anything other than a Numeric String then you will get the CRASH you are asking for!


    Regards Les, Livingston, Scotland

    Monday, October 29, 2018 4:22 PM
  • First, convert the text into a number, as suggested above, and make sure the user input is numeric so it doesn't crash before starting the IF block. 

    After converting the text to a number, you can place this at the beginning of the IF block:

    If score > 100 Or score < 0 Then

    MessageBox.Show(“Incorrect entry. Try again.”)

    'and continue with the rest of the code.

    Also, you don't need any of the ANDs in your IF block.  It will still work without them as long as the score is between 0 and 100.


    Solitaire



    • Edited by Solitaire Monday, October 29, 2018 8:28 PM
    Monday, October 29, 2018 8:26 PM
  • Hi,

    Let the Textbox only enter numbers from 0-100

     Private Sub txt1_TextChanged(sender As Object, e As EventArgs) Handles txt1.TextChanged
            txt1.Text = Regex.Replace(txt1.Text, "[^0-9]", "")
            If txt1.Text = "" Then Return
            Dim num As Integer = Integer.Parse(txt1.Text)
            txt1.Text = num.ToString()
            If num <= 100 Then Return
            'txt1.Text = txt1.Text.Substring(0, txt1.Text.Length - 1)
            MsgBox("Enter a number between 0 and 100")
            txt1.Text = ""
        End Sub

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 30, 2018 6:21 AM
  • updated the code still getting an error

    i added    "   txtAverage.ToString("F1") "

    still getting a crash

    Tuesday, October 30, 2018 1:08 PM
  • PS "genius" don't go being a grammar warrior on the internet... i appreciate the help but if your able to answer my question then you read my sentence fine right? --A holes like u are what make kids not wanna follow their dreams...

    PS

    NEXT time you right PS think before u type

    Hi

    You need to use the Quote to reply to a particular person (or use their name) otherwise we won't know who you are trying to insult.


    Regards Les, Livingston, Scotland

    • Marked as answer by Joshua Cruz 16 Wednesday, October 31, 2018 10:45 PM
    Tuesday, October 30, 2018 10:42 PM
  • PS "genius" don't go being a grammar warrior on the internet... i appreciate the help but if your able to answer my question then you read my sentence fine right? --A holes like u are what make kids not wanna follow their dreams...

    PS

    NEXT time you right PS think before u type

    Hi

    You need to use the Quote to reply to a particular person (or use their name) otherwise we won't know who you are trying to insult.


    Regards Les, Livingston, Scotland

    I did not know that i am sorry his response isnt posted anymore i had reported it...

    I fixed my issue though thank you for the help!

    Tuesday, October 30, 2018 10:46 PM
  • Hi,

    We appreciate that you shared your solution with us. Please mark the post(s) that helped you as the answer.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 31, 2018 1:24 AM