0

每个人:

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

1 回答 1

0

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

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

于 2013-05-09T02:36:50.957 回答