gpt4 book ai didi

vb.net - 防止文本框递归调用textchanged

转载 作者:行者123 更新时间:2023-12-03 08:33:17 25 4
gpt4 key购买 nike

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

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

输入无效消息时,消息框会 pop 两次。这是由于将textbox.text设置回空字符串。我不知道如何防止这种情况发生,如果有人可以帮助清理此凌乱的代码,也将不胜感激。

谢谢!

最佳答案

快速解决方案是,如果文本等于vbNullString,则保留Sub。将这一行放在Sub的开头:

If txtDeadLoadFactor.Text = vbNullString then Exit Sub

假设用户可以手动清空该框,那么如果 pop 窗口不验证空字段会更好。

编辑:此重构的代码可能会帮助您:
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

关于vb.net - 防止文本框递归调用textchanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17434406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com