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

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

问题描述

我想验证用户在提交表单之前总是在文本框中输入一个值.但是我所做的检查允许用户输入空格并继续提交表单.那么,如果文本框中只有空格,如何进行检查以使用户无法提交表单.

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天全站免登陆