小编a.b*_*ell的帖子

使用C#函数检查空文本框

我编写了一个函数来检查表单上的任何文本框是否为空.如果我将它添加到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, "");
        }
    }
Run Code Online (Sandbox Code Playgroud)

只是想知道你是否可以建议我如何解决它.

非常感谢.

c# validation textbox

2
推荐指数
1
解决办法
3万
查看次数

标签 统计

c# ×1

textbox ×1

validation ×1