因此,我创建了一个计算器,可以将您的成绩计算为总体平均水平。但是我有一个问题,如果用户在每个文本框中输入字母而不是数字怎么办?我想防止他们使用字母。那么,限制用户输入字母的代码是什么?我只需要一个按钮的代码。我将感谢您的帮助!
我已经尝试了网站上的所有代码,但是都无法正常工作。
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")
在第二个If语句中,它仅限制以下字符:“ /,^,#等”。我也想限制字母。有人可以帮助我吗?
P.S。我正在使用Windows窗体。
答案 0 :(得分:0)
假设我有3个文本框,分别名为txtMath,txtEnglish和txtScience。当我单击一个按钮时,代码将检查文本框中的所有值是否都可以解析为数字。然后另一个检查所有数字是否都在0到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