【问题标题】:C# Troubleshooting the error provider -- validating text box data inputC# 对错误提供程序进行故障排除——验证文本框数据输入
【发布时间】:2013-05-09 02:35:13
【问题描述】:

大家:

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


【解决方案1】:

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

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

【讨论】:

  • 你是对的。该事件甚至根本没有被调用。不知何故,“验证”事件已从所有文本框中清除,除了一个......正在工作的那个。感谢您的帮助。
  • @INFSstudent115 这些问题总是发生在我身上太多次,让我感到沮丧,直到我把头发从头上拉出来。随着时间的流逝,您会习惯首先检查的内容:) 如果这有帮助,请不要忘记投票/接受答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-04
  • 2019-02-21
相关资源
最近更新 更多