在Windows窗体文本框验证 [英] Textbox validation in a Windows Form

查看:158
本文介绍了在Windows窗体文本框验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个验证的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
    }
}

这篇关于在Windows窗体文本框验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆