【问题标题】:Input Numbers and Unit Grades Only at Textbox (VB.Net)仅在文本框 (VB.Net) 中输入数字和单位成绩
【发布时间】:2016-02-26 15:51:56
【问题描述】:

嗨,我只想在文本框中输入 1 到 5 作为单位成绩 我使用此代码:

Private Sub TextBox16_TextChanged(sender As Object, e As EventArgs) Handles TextBox16.TextChanged
    If TextBox16.Text >= 5 Then
        TextBox16.clear()
        MsgBox("Unit Grades Only From 1 to 5")

    End If
End Sub

结束类

出现错误:

Microsoft.VisualBasic.dll 中出现“System.InvalidCastException”类型的未处理异常

附加信息:从字符串“”到类型“Double”的转换无效。

【问题讨论】:

  • a) 给你的控件起有意义的名字——你被命名为“Child16”还是类似的名字? b) 打开选项 strict - 将文本与数字进行比较是无效的,因为字符串不是数字。与其监视每一次击键,不如在完成后点亮并检查所有输入,然后单击“确定”或其他任何内容。你不需要因为错字而责骂他们
  • 如果他们只应该输入 1-5(整数),请使用 NumericUpDown
  • If Integer.Parse(TextBox16.Text) >= 5 Then

标签: vb.net


【解决方案1】:

好吧,您有两种方法可以做到这一点:

防止输入它们
在这种情况下,您将拒绝用户输入其他字符。所以编码:

Private Sub TextBox16_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox16.KeyPress
'Deny user from entering more than one charecter
    TextBox1.MaxLength = 1
    Select Case e.KeyChar
        Case "1", "2", "3", "4", "5" ,vbBack 'vbBack is backspace
            'You can enter any number that you whant
            e.Handled = False
        Case Else
            'Ignore the key
            e.Handled = True
            'Play a Beep sound.
            Beep()
        End Select
End Sub

使用 TextChanged 检查
在你的方法中输入这个:

Dim textbox_val As Integer
'The user may enter alphabetical characters
Try
    textbox_val = Convert.ToInt32(TextBox1.Text)
Catch ex As Exception
    MsgBox("Unit Grades Only From 1 to 5")
    Exit Sub
End Try
If textbox_val >= 5 Then
    TextBox1.Clear()
    MsgBox("Unit Grades Only From 1 to 5")
End If

【讨论】:

    【解决方案2】:

    如果您决定为此使用文本框,则以下代码应防止文本框中出现 1 到 5 以外的任何内容。我使用了一个命名更好的文本框 txtUnitGrade。

    Private _sLastValidValue As String = String.Empty
    Private _bIgnoreChange As Boolean = False
    
    Private Sub txtUnitGrade_TextChanged(sender As Object, e As EventArgs) Handles txtUnitGrade.TextChanged
    
    If Not _bIgnoreChange Then
        If txtUnitGrade.Text = String.Empty Then
            _sLastValidValue = String.Empty
        Else
            If Not IsNumeric(txtUnitGrade.Text) Then
                SetValueToLast()
            Else
                Dim iParsedValue As Integer = Integer.MinValue
                If Not (Integer.TryParse(txtUnitGrade.Text, iParsedValue)) OrElse iParsedValue < 0 OrElse iParsedValue > 5 Then
                    SetValueToLast()
                Else
                    _sLastValidValue = txtUnitGrade.Text
                End If
            End If
        End If
    End If
    End Sub
    
    Private Sub SetValueToLast()
        Beep() 'you could add some other audible or visual cue that an block occured
        _bIgnoreChange = True
        txtUnitGrade.Text = _sLastValidValue
        txtUnitGrade.SelectionStart = txtUnitGrade.Text.Length
        _bIgnoreChange = False
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多