Vb.net 防止用户为所有文本框输入字母

Vb.net 防止用户为所有文本框输入字母,vb.net,Vb.net,所以我发明了一个计算器,可以把你的分数计算成你的平均分。但我有一个问题,如果用户在每个文本框中输入字母而不是数字,会怎么样?我想阻止他们使用字母。那么限制用户输入字母的代码是什么?我只需要一个按钮的代码。我会感谢你的帮助 我试过网站上的所有代码,但都不起作用 如果清空文本框,则 MsgBox(“请在按下“计算”之前填写所有框”,MsgBoxStyle.Critical,“GWA计算器”) ElseIf Math.Text.Count(函数(c)非Char.IsLetterOrDigit(c))>

所以我发明了一个计算器,可以把你的分数计算成你的平均分。但我有一个问题,如果用户在每个文本框中输入字母而不是数字,会怎么样?我想阻止他们使用字母。那么限制用户输入字母的代码是什么?我只需要一个按钮的代码。我会感谢你的帮助

我试过网站上的所有代码,但都不起作用

如果清空文本框,则
MsgBox(“请在按下“计算”之前填写所有框”,MsgBoxStyle.Critical,“GWA计算器”)
ElseIf Math.Text.Count(函数(c)非Char.IsLetterOrDigit(c))>0或Eng.Text.Count(函数(c)非Char.IsLetterOrDigit(c))>0或
Sci.Text.Count(函数(c)不是Char.IsLetterOrDigit(c))>0或Fil.Text.Count(函数(c)不是Char.IsLetterOrDigit(c))>0或
TL.Text.Count(函数(c)不是Char.IsLetterOrDigit(c))>0或IC.Text.Count(函数(c)不是Char.IsLetterOrDigit(c))>0或
MAP.Text.Count(函数(c)不是Char.IsLetterOrDigit(c))>0或Araling.Text.Count(函数(c)不是Char.IsLetterOrDigit(c))>0或
ES.Text.Count(函数(c)不是Char.IsleterOrdigit(c))>0然后
MsgBox(“其中一个框包含无效字符”,MsgBoxStyle.Critical,“GWA计算器”)
ElseIf(Math.Text.Length 2)或(Eng.Text.Length 2)或(Sci.Text.Length 2)或(Fil.Text.Length 2)或
(TL.Text.Length 2)或(IC.Text.Length 2)或(MAP.Text.Length 2)或(Araling.Text.Length 2)或
(ES.Text.Length 2)然后
MsgBox(“其中一个框包含1或3位数字。它必须包含2位数字才能进行计算。”,MsgBoxStyle.Critical,“GWA计算器”)
在第二个If语句中,它只限制这类字符:“/,^,#,等等。”我还想限制字母。有人能帮我吗


另外,我使用的是Windows窗体。

假设我有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
Private子按钮1\u单击(发送者作为对象,e作为事件参数)处理按钮1。单击
Dim mathScore、EnglishCore、科学分数为整数
'解析为数字
模糊记分器=
Integer.TryParse(txtMath.Text,mathScore)和
整数.TryParse(txtEnglish.Text,EnglishCore)和
整数.TryParse(txtScience.Text,scienceScore)
'介于0和99之间
模糊记分器=
(mathScore>=0且mathScore<100)且
(英语核心>=0,英语核心<100)和
(科学分数>=0,同时科学分数<100)
如果不是记分员,那么
MsgBox(“其中一个值不是数字”)
否则就不得分了
MsgBox(“其中一个值不在0和99之间”)
其他的
模糊分数=
“Math={0}”&vbNewLine&
“English={1}”&vbNewLine&
“科学={2}”
MsgBox(String.Format(FormatScore、mathScore、EnglishCore、scienceScore))
如果结束
端接头

请解释。这是ASP.NET应用程序还是桌面应用程序?(WinForms,WPF)我正在使用Windows窗体。看到这个了吗。韩,我需要把这个代码放在每个文本框中吗?因为我只需要一个按钮的代码就可以完成所有文本框的工作。@Han,现在可以了。谢谢你的帮助!