Foreach 循环遍历文本框控件不会返回 [英] Foreach loop through controls that are textbox doesn't return

查看:27
本文介绍了Foreach 循环遍历文本框控件不会返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前在另一个程序中使用过这段代码,但现在我无法理解为什么它不会在我的第二行之后运行代码.

I've used this code before in another program, but now I'm having trouble understanding why it won't run the code after my second line.

foreach (Control c in Controls)
    if (c.GetType() == typeof(TextBox)) //doesn't run any further
    {
        if ((string)c.Tag == "Filled")
        {
            ...
        }
        ...
    }

我要么遗漏了一些小细节,要么其他地方不正确.有什么想法吗?

I'm either missing some minor little detail or something else is incorrect. Any ideas?

我的文本框在面板内.

推荐答案

当你调用 Control.Controls 时,它只会返回最外层的控件.它不会递归下降到任何包含其他控件的容器控件中.

When you call Control.Controls, it will only return the controls at the outermost level. It won't recursively descend into any container controls that hold other controls.

如果您的控件在另一个容器中,您将需要使用该容器的 .Controls 属性.

If your controls are in another container, you will need to use that container's .Controls property instead.

或者,您可以通过编写一个方法来概括它,以递归地返回父级及其所有子级的所有控件,如下所示:

Alternatively you can generalize it by writing a method to recursively return all the controls from the parent and all it's children, like so:

public IEnumerable<Control> AllControls(Control container)
{
    foreach (Control control in container.Controls)
    {
        yield return control;

        foreach (var innerControl in AllControls(control))
            yield return innerControl;
    }
}

然后您可以使用它代替 Control.Controls,如下所示:

You can then use that instead of Control.Controls as follows:

private void test() // Assuming this is a member of a Form other class derived from Control
{
    var textboxesWithFilledTag = 
        AllControls(this).OfType<TextBox>()
        .Where(tb => (string) tb.Tag == "Filled");

    foreach (var textbox in textboxesWithFilledTag)
        Debug.WriteLine(textbox.Text);
}

正如评论所说,我假设 test() 方法是您的 Form 或从 Control 派生的另一个类的成员.如果不是,则必须将父控件传递给它:

As the comment says, I'm assuming that the test() method is a member of your Form or another class derived from Control. If it isn't, you will have to pass the parent control to it:

private void test(Control container)
{
    var textboxesWithFilledTag = 
        AllControls(container).OfType<TextBox>()
        .Where(tb => (string) tb.Tag == "Filled");

    foreach (var textbox in textboxesWithFilledTag)
        Debug.WriteLine(textbox.Text);
}

以下方法与上述方法具有相同的结果,供参考(恕我直言,可读性更高):

The following method has identical results to the one above, for reference (and is more readable IMHO):

private void test(Control container)
{
    foreach (var textbox in AllControls(container).OfType<TextBox>())
        if ((string)textbox.Tag == "Filled")
            Debug.WriteLine(textbox.Text);
}

<小时>

对于您的代码,您的按钮点击处理程序可能如下所示:


For your code, your button click handler might look something like this:

void button1_Click(object sender, EventArgs e)
{
    foreach (var c in AllControls(this).OfType<TextBox>())
    {
        if ((string) c.Tag == "Filled")
        {
            // Here is where you put your code to do something with Textbox 'c'
        }
    }
}

请注意,您当然还需要 AllControls() 方法.

Note that you also need the AllControls() method, of course.

这篇关于Foreach 循环遍历文本框控件不会返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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