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

本文关键字:验证 排除 文本 输入 数据 故障 错误 程序 | 更新日期: 2023-09-27 18:12:59

所有人:

我正在尝试验证表单上文本框中的数据。这是我为验证器准备的代码,但当我明显违反了代码中设置的约束时(从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
        }

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

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

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