Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 防止textbox递归调用textchanged_Vb.net_Error Handling_Textbox - Fatal编程技术网

Vb.net 防止textbox递归调用textchanged

Vb.net 防止textbox递归调用textchanged,vb.net,error-handling,textbox,Vb.net,Error Handling,Textbox,使用以下代码检查文本框中的用户输入时出错: Private Sub txtDeadLoadFactor_TextChanged(sender As Object, e As EventArgs) Handles txtDeadLoadFactor.TextChanged Dim invalidEntry As Boolean If IsNumeric(txtDeadLoadFactor.Text) And Not txtDeadLoadFactor.Text =

使用以下代码检查文本框中的用户输入时出错:

Private Sub txtDeadLoadFactor_TextChanged(sender As Object, e As EventArgs) Handles txtDeadLoadFactor.TextChanged
        Dim invalidEntry As Boolean
        If IsNumeric(txtDeadLoadFactor.Text) And Not txtDeadLoadFactor.Text = vbNullString Then
            If Not txtDeadLoadFactor.Text > 0 Then
                invalidEntry = True
            End If
        Else
            invalidEntry = True
        End If
        If invalidEntry Then
            MsgBox("Please only enter numeric data greater than 0 in all fields!", MsgBoxStyle.Critical, "Invalid Input!")
            txtDeadLoadFactor.Text = vbNullString
            invalidEntry = False
        Else
            gDeadLoadFactor = txtDeadLoadFactor.Text
        End If
    End Sub
当输入无效时,消息框会弹出两次。这是由于将textbox.text设置回空字符串。我不知道如何防止这种情况发生,如果有人能帮我清理这些乱七八糟的代码,我将不胜感激


谢谢大家!

如果文本等于vbNullString,则快速修复方法是离开Sub。将这一行放在子标题的开头:

If txtDeadLoadFactor.Text = vbNullString then Exit Sub
考虑到用户可以手动清空该框,如果弹出窗口不验证空字段会更好

编辑:此重构代码可能会帮助您:

If txtDeadLoadFactor.Text = vbNullString then Exit Sub
If Not(IsNumeric(txtDeadLoadFactor.Text)) OrElse txtDeadLoadFactor.Text <= 0 then
    MsgBox("Please only enter numeric data greater than 0 in all fields!", MsgBoxStyle.Critical, "Invalid Input!")
    txtDeadLoadFactor.Text = vbNullString
    Exit Sub
End If

gDeadLoadFactor = txtDeadLoadFactor.Text
如果txtDeadLoadFactor.Text=vbNullString,则退出Sub

如果不是(IsNumeric(txtDeadLoadFactor.Text))或lse txtDeadLoadFactor.Text为什么要将文本设置为空白?只需将文本保留在错误消息框中,这样用户就知道哪些输入是不允许的,如果他们再次尝试,将得到相同的结果(即一致性,而如果您更改了他们的输入,那么当他们再次单击提交时,将得到不同的错误)。只有我的两分钱。

我不知道我为什么不这么做。这是漫长的一天。非常感谢。