在Windows窗体文本框验证窗体、文本框、Windows

由网友(不爱别道歉ゝ直接滚)分享简介:我想提出一个验证的submiting表格前,用户总是进入在文本框中的值。但是,我已经把支票允许用户输入空格,继续提交表单。那么,如何把检查,以便在无法对用户提交表单,如果有在文本框中只有空格。I want to put a validation that the user always enters a value...

我想提出一个验证的submiting表格前,用户总是进入在文本框中的值。但是,我已经把支票允许用户输入空格,继续提交表单。 那么,如何把检查,以便在无法对用户提交表单,如果有在文本框中只有空格。

I want to put a validation that the user always enters a value in the textbox before submiting the form. But the check that I have put allows user to enter white spaces and continue submitting the form. So, how to put the check so that the user in not able to submit the form if there are only white spaces in the textbox.

推荐答案

您可以创建自己的自定义验证函数。这可能是非常幼稚的,但不知何故,它会奏效。

You can make your own custom validation function. This may be very naive, but somehow it will work.

private bool WithErrors()
{
    if(textBox1.Text.Trim() == String.Empty) 
        return true; // Returns true if no input or only space is found
    if(textBox2.Text.Trim() == String.Empty)
        return true;
    // Other textBoxes.

    return false;
}

private void buttonSubmit_Click(object sender, EventArgs e)
{
    if(WithErrors())
    {
        // Notify user for error.
    }
    else
    {
        // Do whatever here... Submit
    }
}
阅读全文

相关推荐

最新文章