-1

So I've created a calculator that computes your grades into your general average. But I have one problem, what if the user entered letters in each textboxes instead of numbers? I want to prevent them to use letters. So what're the codes for restricting the user from entering letters? I just need a code for a button. I will appreciate the help!

I've tried all the codes from the websites but all of them didn't work.

        If emptyTextBoxes.Any Then
            MsgBox("Please fill all the boxes before you press 'Compute'.", MsgBoxStyle.Critical, "GWA Calculator")
        ElseIf Math.Text.Count(Function(c) Not Char.IsLetterOrDigit(c)) > 0 Or Eng.Text.Count(Function(c) Not Char.IsLetterOrDigit(c)) > 0 Or
            Sci.Text.Count(Function(c) Not Char.IsLetterOrDigit(c)) > 0 Or Fil.Text.Count(Function(c) Not Char.IsLetterOrDigit(c)) > 0 Or
            TL.Text.Count(Function(c) Not Char.IsLetterOrDigit(c)) > 0 Or IC.Text.Count(Function(c) Not Char.IsLetterOrDigit(c)) > 0 Or
            MAP.Text.Count(Function(c) Not Char.IsLetterOrDigit(c)) > 0 Or Araling.Text.Count(Function(c) Not Char.IsLetterOrDigit(c)) > 0 Or
            ES.Text.Count(Function(c) Not Char.IsLetterOrDigit(c)) > 0 Then
            MsgBox("One of the boxes has invalid characters", MsgBoxStyle.Critical, "GWA Calculator")
        ElseIf (Math.Text.Length <> 2) Or (Eng.Text.Length <> 2) Or (Sci.Text.Length <> 2) Or (Fil.Text.Length <> 2) Or
            (TL.Text.Length <> 2) Or (IC.Text.Length <> 2) Or (MAP.Text.Length <> 2) Or (Araling.Text.Length <> 2) Or
            (ES.Text.Length <> 2) Then
            MsgBox("One of the boxes contains 1 or 3 digits. It must contain 2 digits only to be computed.", MsgBoxStyle.Critical, "GWA Calculator")

At the second If statement, it only restricts this kind of characters: "/, ^, #, etc." I want to restrict letters also. Anyone can help me?

P.S. I'm using Windows Forms.

sccooottttie
  • 27
  • 1
  • 1
  • 9

1 Answers1

0

Assume I have 3 textboxes named txtMath, txtEnglish and txtScience for the numbers. When I click a button, the code checks if all the values in the textboxes can be parsed into numbers. Then another checks if all the numbers are between 0 and 99.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim mathScore, englishScore, scienceScore As Integer

    ' parse into numbers
    Dim scoresAreNumbers =
        Integer.TryParse(txtMath.Text, mathScore) AndAlso
        Integer.TryParse(txtEnglish.Text, englishScore) AndAlso
        Integer.TryParse(txtScience.Text, scienceScore)

    ' between 0 and 99
    Dim scoresAreValid =
        (mathScore >= 0 AndAlso mathScore < 100) AndAlso
        (englishScore >= 0 AndAlso englishScore < 100) AndAlso
        (scienceScore >= 0 AndAlso scienceScore < 100)

    If Not scoresAreNumbers Then
        MsgBox("One of the values are not numbers")
    ElseIf Not scoresAreValid Then
        MsgBox("One of the values are not between 0 and 99")
    Else
        Dim formatScores =
            "Math = {0}" & vbNewLine &
            "English = {1}" & vbNewLine &
            "Science = {2}"
        MsgBox(String.Format(formatScores, mathScore, englishScore, scienceScore))
    End If

End Sub
Han
  • 3,052
  • 2
  • 22
  • 31