VB - 按下退格键后,TextChanged 没有正确触发?

Gro*_*247 -1 vb.net

我的问题归结为:

我有六个文本框,它们期望一个介于 0 和给定数字之间的值。我想要实现的是:

  • 如果输入的数字介于 0 和指定数字之间(作为标签),文本将保持黑色
  • 如果输入的数字超过指定的数字,文本将变为红色

这里的问题是,如果指定的数字是“10”,而用户输入 11,它会变成红色,这是应该的,但是,如果他们按下退格键(输入的数字现在是 1),数字仍然是红色的,即不是预期的功能 - 数字 1 应该是黑色的,因为它介于 0 和指定的数字之间。

所有指定的数字现在都是硬编码的(我正在学习初学者课程,这只是我为了增加程序功能而做的事情,我还没有为每个“作业”添加类" ) 并且您可以在技术上输入负数,我目前不在乎。

这是作为特定 GroupBox 中所有文本框的处理程序添加的子例程

' Handler which gets added to all TextBoxes in "grpGrades" GroupBox
    Private Sub txtGradePoints_TextChanged(sender As Object, e As EventArgs)

        ' Take in generic sender (Textbox) and convert to TextBox (necessary due to Strict mode)
        Dim textBox = CType(sender, TextBox)
        Try
            ' the value of the current TextBox being checked
            Dim val = Decimal.Parse(textBox.Text)
            Select Case textBox.Name
                Case "txtPostPoints"
                    If val > 10 Then textBox.ForeColor = Color.Red
                Case "txtCh1TestPoints", "txtCh2TestPoints", "txtCh3TestPoints"
                    If val > 50 Then textBox.ForeColor = Color.Red
                Case "txtCh2TutPoints", "txtCh3TutPoints"
                    If val > 25 Then textBox.ForeColor = Color.Red
                Case Else
                    textBox.ForeColor = Color.Black

            End Select
        Catch
            textBox.ForeColor = SystemColors.ControlText
        End Try


    End Sub
Run Code Online (Sandbox Code Playgroud)

这是 onLoad 处理程序,它从“grpGrades”GroupBox 获取适当的 TextBox 控件,并将上述 TextChanged 处理程序添加到每个控件。

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

        ' Get array of TextBox Controls from the "grpGrades" GroupBox
        Dim textBoxes = grpGrades.Controls.OfType(Of TextBox)()

        ' Go through the array of TextBoxes and add the TextChanged handler to each TextChanged event
        For Each txt In textBoxes
            AddHandler txt.TextChanged, AddressOf txtGradePoints_TextChanged
        Next

        'AddHandler txtPostPoints.TextChanged, AddressOf txtGradePoints_TextChanged
        'AddHandler txtCh1TestPoints.TextChanged, AddressOf txtGradePoints_TextChanged
        'AddHandler txtCh2TestPoints.TextChanged, AddressOf txtGradePoints_TextChanged
        'AddHandler txtCh3TestPoints.TextChanged, AddressOf txtGradePoints_TextChanged
        'AddHandler txtCh2TutPoints.TextChanged, AddressOf txtGradePoints_TextChanged
        'AddHandler txtCh3TutPoints.TextChanged, AddressOf txtGradePoints_TextChanged
    End Sub
Run Code Online (Sandbox Code Playgroud)

子程序的最后一部分只是注释掉的代码,也是我最初添加处理程序的方式,以防万一我的新方法出现问题。

编辑:是否有必要拒绝投票?是什么原因?

Ste*_*eve 5

您的代码从不测试有效值。当当前文本框返回有效值时,将当前文本框设置为黑色的 Case Else 永远不会被命中。这不会发生,因为 Select Case 将匹配文本框的当前 Name,将再次测试无效值,然后退出 Select Case 块。您需要为当前文本框名称的适当大小写中的有效值设置颜色。该IF条件运算符可以一切减少到单行

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

    ' Take in generic sender (Textbox) and convert to TextBox (necessary due to Strict mode)
    Dim textBox = CType(sender, TextBox)
    Try
        ' the value of the current TextBox being checked
        Dim val = Decimal.Parse(textBox.Text)
        Select Case textBox.Name
            Case "txtPostPoints"
                textBox.ForeColor = IF(val > 10, Color.Red, Color.Black)
            Case "txtCh1TestPoints", "txtCh2TestPoints", "txtCh3TestPoints"
                textBox.ForeColor = IF(val > 50, Color.Red, Color.Black)
            Case "txtCh2TutPoints", "txtCh3TutPoints"
                textBox.ForeColor = IF(val > 25, Color.Red, Color.Black)
            Case Else
                ' Not sure if it is needed for other textboxes....
                textBox.ForeColor = Color.Black

        End Select
    Catch
        textBox.ForeColor = SystemColors.ControlText
    End Try


End Sub
Run Code Online (Sandbox Code Playgroud)