【发布时间】:2011-04-09 14:39:33
【问题描述】:
我编写了一个函数来检查表单上的任何文本框是否为空白。 如果我将它添加到 TextBox 'leave' 事件,它目前可以工作。
我尝试将它添加到按钮单击事件中,但它给出了错误 (NullReferenceException unhandled)。
下面是代码:
public void inputBlank(object sender, EventArgs e)
{
TextBox userInput;
userInput = sender as TextBox;
userTextBox = userInput.Text;
string blankBoxName = userInput.Name;
string blankBox = blankBoxName.Remove(0,3);
if (userTextBox == "")
{
errWarning.SetError(userInput, "Please enter a value for " + blankBox);
userInput.Focus();
}
else
{
errWarning.SetError(userInput, "");
}
}
只是想知道您是否可以建议我如何解决它。
非常感谢。
【问题讨论】:
-
错误的sender,它是一个Button,而不是一个TextBox。
标签: c# validation textbox