在所有文本框中检查许多文本框是否为null [英] check many textbox for null in all of textboxes

查看:87
本文介绍了在所有文本框中检查许多文本框是否为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

i有一个关于检查空文本框的问题,

i希望检查表单上所有文本框中的多个文本框,

我该怎么办?

i使用这段代码,但它检查表格上的所有文本框。

hi everyone,
i have a question about checking text boxes for null,
i want to check many text box in all text boxes on the form,
how can i do it?
i use this code but it check all text boxes on the form.

private void button1_Click(object sender, EventArgs e)
        {
            bool isIncomplete = false;
            foreach (Control ctls in this.Controls)
            {
                if (ctls is TextBox)
                {
                    TextBox tb = ctls as TextBox;
                    if(string.IsNullOrWhiteSpace(tb.Text))
                    {
                        isIncomplete = true;
                        break;
                    }

                }


            }
            if (isIncomplete)
            {
                MessageBox.Show("please fill all text boxes!");
            }
        }

推荐答案

如果您只想检查一些文本框,而不是全部然后你需要有一些方法来指定检查这一个或不要检查这一个。最简单的方法之一是使用Control.Tag属性。



默认情况下,Tag为null,因此您可以为那些值赋值我想检查,(或只是你不喜欢的人 - 你喜欢哪个)。您可以在设计或运行时设置标记,它可以包含您想要的任何对象。

If you only want to check some of the text boxes, instead of all of them then you need to have some way to specify "check this one" or "Don't check this one". One of teh simplest ways to do that is to use the Control.Tag property.

By default, the Tag is null, so you could give a value to those you do want to check, (or just to those you don't - whichever you prefer). You can set the Tag at design or run time, and it can contain any object you want it to.
foreach (Control c in Controls)
    {
    if (c.Tag != null && c is TextBox)
        {
        TextBox t = c as TextBox;
        if (string.IsNullOrWhiteSpace(t.Text))
            {
            isIncomplete = true;
            break;
            }
        }
    }


如果将其添加到某个控件中,则无法使用空复选框您的用户界面。从代码中可以看出,您正在尝试检查属性 System.Window.Forms.TextBox.Text ,但此文本也永远不会为null,因此所有你需要的是通过与 string.Empty 比较来检查它(当然不是空的,只是空字符串!)。



您的代码缺少递归。问题是:某些文本框不能直接放在表单上,​​而是放在其他一些控件(面板)上。实际上,建议将所有内容放在某些层次结构的面板上并使用对接。作为一个拇指规则,永远不要使用手动放置。



要考虑到它,你的代码应该是这样的:



It is impossible to have a null check box, if it is added to some control of your UI. As it can be seen from your code, you are trying to check up the property System.Window.Forms.TextBox.Text, but this text is also never null, so all you need is to check it by comparison with string.Empty (which is of course not null, just empty string!).

Your code lacks recursion. The problem is: some of the text boxes can be placed not directly on form, but on some other controls (panels). Actually, it's recommended to put everything on some hierarchy of panels and use docking. As a matter of thumb rule, never use manual placement.

To take it into account, your code should be something like that:

static TextBox FindEmptyTextBox(Control parent) {
    TextBox textBox = parent as TextBox;
    if (textBox != null) { // successful dynamic cast, parent is a TextBox
        it (textBox.Text == string.Empty) // variant: if (textBox.Text.Trim() == string.Empty)
            return textBox;
    } else
        foreach(Control child in parent.Controls) {
            TextBox childTextBox = FindEmptyTextBox(child);
            if (childTextBox != null)
                return childTextBox;
        } //loop
    return null;
} // FindEmptyTextBox





此代码将按层次结构找到遇到的第一个空文本框。它返回null,没有控件的父级文本框,或者它们都是非空的。如果将此方法应用于表单,它将按层次结构找到此表单上第一个遇到的文本框。引用找到的控件可能很重要,因为您可以专注于它并告诉用户在其中输入文本(而不是向用户提供难看的建议以找到所有这些)。



-SA


这篇关于在所有文本框中检查许多文本框是否为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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