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