【问题标题】:Invalid input when integer is within valid means?当整数在有效平均值内时输入无效?
【发布时间】:2015-09-03 02:46:35
【问题描述】:

我正在开发一个相当基本的 VB 程序(没有双关语),我需要在其中将基本整数转换为罗马数字。我的转换部分与我的 Select Case 完美配合。我还需要添加验证输入,因此如果输入了无效数字,则文本框会显示为这样。 1 到 10 之间的任何数字都应该能够单击转换按钮。目前,我输入 1 到 10 之间的任何数字都会立即显示“该数字无效。”

这是我当前的代码,失败了:

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Private Sub lblRomanNum_Click(sender As Object, e As EventArgs)

End Sub

Private Sub txtBox1_TextChanged(sender As Object, e As EventArgs) Handles txtBox1.TextChanged
    Dim intNum As Integer
    If intNum < 1 Or intNum > 10 Then
        txtBox1.Text = "That number is invalid."
        'ElseIf intNum > 10 Then
        'txtBox1.Text = "That number is invalid"
    End If

End Sub

Private Sub txtBox2_TextChanged(sender As Object, e As EventArgs) Handles txtBox2.TextChanged

End Sub

Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
Select CInt(txtBox1.Text)
        Case 1                      ' numerical 1
            txtBox2.Text = "I"
        Case 2                      ' numerical 2
            txtBox2.Text = "II"
        Case 3                      ' numerical 3
            txtBox2.Text = "III"
        Case 4                      ' numerical 4
            txtBox2.Text = "IV"
        Case 5                      ' numerical 5
            txtBox2.Text = "V"
        Case 6                      ' numerical 6
            txtBox2.Text = "VI"
        Case 7                      ' numerical 7
            txtBox2.Text = "VII"
        Case 8                      ' numerical 8
            txtBox2.Text = "VIII"
        Case 9                      ' numerical 9
            txtBox2.Text = "IX"
        Case 10                     ' numerical 10
            txtBox2.Text = "X"
            'Case Else
            'If a user enters an invalid value, this message is displayed and no conversion is attempted, according to instructions.
            'txtBox2.Text = "That value is invalid."
    End Select

End Sub

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
    Me.Close()
End Sub

Private Sub lblRomanNum_Click_1(sender As Object, e As EventArgs)

End Sub
End Class

任何小于 1 的 intNum 都应显示无效消息。

任何大于 10 的 intNum 都应显示无效消息。

如果我正在正确阅读我目前拥有的内容,这应该可以工作,并允许我输入 1 到 10 之间的数字,而不会出现无效消息。我在这里遗漏了什么吗?

【问题讨论】:

    标签: vb.net vba


    【解决方案1】:

    试试括号和“或”

    1. Dim intNum As Integer
    2.    If (intNum < 1) or (intNum > 10) Then
    3.        txtBox1.Text = "That number is invalid."
    4.    End If
    

    【讨论】:

    • 莫名其妙,您的建议仍然导致无效消息。
    • 那么你需要在另一个控件中显示你得到的值;也许它不是你认为应该的那样。
    【解决方案2】:

    您的代码在带有 .NET Framework 4.5.1 的 Visual Studio 2013 中非常适合我。尝试删除整个块并重新键入代码。 或者你也可以试试这个代码

     If intNum < 1 OrElse intNum > 10 Then
        TextBox1.Text = "That number is invalid."
     End If
    

    【讨论】:

    • 莫名其妙,您的建议仍然导致无效消息。
    • 您已更改问题。 Dim intNum As Integer 是声明,intNum 始终为 0。因此代码按预期工作。
    【解决方案3】:

    需要添加...

     Integer.TryParse(txtBox1.Text, i) 
    

    ...直接在我的变量声明下。

    【讨论】:

      猜你喜欢
      • 2019-07-24
      • 2021-02-10
      • 2020-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-15
      • 2017-09-03
      相关资源
      最近更新 更多