c# - C#对错误提供程序进行故障排除-验证文本框数据输入

标签 c# error-handling validation

大家:

我正在尝试验证表单上文本框中的数据。这是我为验证器提供的代码,但是当我明显违反代码中设置的约束(从txtbox中跳出,输入少于4个字符等)时,它就无法正常工作。为什么这行不通?程序运行,所以它不能是语法,所以我想它必须是逻辑错误,但我只是看不到它。

            // Validation for Applicant Name text box
    private void txtAppInfoName_Validating(object sender, CancelEventArgs e)
        {

            // Define what consitiutes a match
            Match m = Regex.Match(txtAppInfoName.Text, @"\b[A-Za-z]\b");

            if (m.Success == false)
            {
                errorProvider1.SetError(txtAppInfoName, "Please use only letters to type your name.");
                txtAppInfoName.Select();
            }

            else if (txtAppInfoName.Text.Length < 4)
            {
                errorProvider1.SetError(txtAppInfoName, "Please type first and last names.");
                txtAppInfoName.Select();
            }

            else if (txtAppInfoName.Text == "")
            {
                errorProvider1.SetError(txtAppInfoName, "Must enter a name.");
                txtAppInfoName.Select();
            }
            else errorProvider1.SetError(txtAppInfoName, ""); // Remove the error provider
        }

最佳答案

很有可能根本没有调用该处理程序。确保在GUI设计器中的txtAppInfoName_Validating事件下指定了处理程序Validating

如果这不起作用,请尝试添加断点或在处理程序中引发异常,然后看看会发生什么。
如果停止,则可以使用调用堆栈检查错误源。

关于c# - C#对错误提供程序进行故障排除-验证文本框数据输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16453436/

相关文章:

c# - 一个线程可以与另一个线程通信吗?

c# - 覆盖 NServiceBus 中的依赖项

c# - 使用 System.Net.NetworkInformation,有没有办法将 ping 绑定(bind)到特定接口(interface)?

javascript - 在未加载图片时,是否可以使用代码来提醒用户?

javascript - Mongoose 和多次保存错误处理

Javascript 表单验证突出显示无效字符

javascript - 如何调用自定义约束验证作为 native 验证事件流的一部分?

c# - 使用 wsdl.exe 从 c# 客户端到 Tomcat 的 HTTPS 连接

bash - 在bash中包装一个perl脚本-向IRC机器人回显

javascript - 服务器和客户端验证器 - 库