简体   繁体   English

如何清除表单中所有textBoxes的文字?

[英]How to clear the text of all textBoxes in the form?

private void CleanForm()
{
    foreach (var c in this.Controls)
    {
        if (c is TextBox)
        {
            ((TextBox)c).Text = String.Empty;
        }
    }
}

This method above doesn't work and the controls aren't cleared.上面的这个方法不起作用,控件没有被清除。 It compiles fine, but does nothing.它编译得很好,但什么也不做。

Any ideas?有任何想法吗?

I like lambda :)我喜欢 lambda :)

 private void ClearTextBoxes()
 {
     Action<Control.ControlCollection> func = null;

     func = (controls) =>
         {
             foreach (Control control in controls)
                 if (control is TextBox)
                     (control as TextBox).Clear();
                 else
                     func(control.Controls);
         };

     func(Controls);
 }

Good luck!祝你好运!

We had a problem like this some weeks before.几周前我们遇到了这样的问题。 If you set a breakpoint and have a deep look into this.Controls , the problem reveals it's nature: you have to recurse through all child controls.如果您设置一个断点并深入研究this.Controls ,问题就会揭示它的本质:您必须递归遍历所有子控件。

The code could look like this:代码可能如下所示:

private void CleanForm()
{
    traverseControlsAndSetTextEmpty(this);
}
private void traverseControlsAndSetTextEmpty(Control control)
{

    foreach(var c in control.Controls)
    {
        if (c is TextBox) ((TextBox)c).Text = String.Empty;
        traverseControlsAndSetTextEmpty(c);
    }
}
private void CleanForm(Control ctrl)
{
    foreach (var c in ctrl.Controls)
    {
        if (c is TextBox)
        {
            ((TextBox)c).Text = String.Empty;
        }

        if( c.Controls.Count > 0)
        {
           CleanForm(c);
        }
    }
}

When you initially call ClearForm, pass in this, or Page (I assume that is what 'this' is).当您最初调用 ClearForm 时,传入 this 或 Page(我假设这就是“this”)。

I improved/fixed my extension method.我改进/修复了我的扩展方法。

public  static class ControlsExtensions
{
    public static void ClearControls(this Control frm)
    {
        foreach (Control control in frm.Controls)
        {
            if (control is TextBox)
            {
                control.ResetText();
            }

            if (control.Controls.Count > 0)
            {
                control.ClearControls();
            }
        }
    }
}

Your textboxes are probably inside of panels or other containers, and not directly inside the form.您的文本框可能位于面板或其他容器内,而不是直接位于表单内。

You need to recursively traverse the Controls collection of every child control.您需要递归遍历每个子控件的Controls集合。

Maybe you want more simple and short approach.也许你想要更简单和简短的方法。 This will clear all TextBoxes too.这也将清除所有文本框。 (Except TextBoxes inside Panel or GroupBox). (Panel 或 GroupBox 内的 TextBoxes 除外)。

 foreach (TextBox textBox in Controls.OfType<TextBox>())
    textBox.Text = "";

And this for clearing all controls in form like textbox, checkbox, radioButton这用于清除表单中的所有控件,如文本框、复选框、单选按钮

you can add different types you want..你可以添加你想要的不同类型..

private void ClearTextBoxes(Control control)
    {
        foreach (Control c in control.Controls)
        {
            if (c is TextBox)
            {
                ((TextBox)c).Clear();
            }

            if (c.HasChildren)
            {
                ClearTextBoxes(c);
            }


            if (c is CheckBox)
            {

                ((CheckBox)c).Checked = false;
            }

            if (c is RadioButton)
            {
                ((RadioButton)c).Checked = false;
            }
        }
    }

Since none of the answers here helped me, I'm posting the solution that worked for me.由于这里的答案都没有帮助我,所以我发布了对我有用的解决方案。

This is applicable when you have textboxes within groupboxes, so you cannot operate using a basic this.Control这适用于组框内有文本框的情况,因此您不能使用基本的this.Control进行操作

you will need to create a double foreach secuence that loops into each groupbox looking for textboxes.您将需要创建一个双 foreach secuence,循环到每个组框以查找文本框。

try using this:尝试使用这个:

public void CleanTxTBoxes()
    {
        foreach (var groupbox in this.Controls.OfType<GroupBox>())
        {
            foreach (var textboxitem in groupbox.Controls.OfType<TextBox>())
            {
                textboxitem.Clear();
            }
        }
    }

Try this:试试这个:

var t = this.Controls.OfType<TextBox>().AsEnumerable<TextBox>();
foreach (TextBox item in t)
{
    item.Text = "";
}

You can try this code你可以试试这个代码

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {


        if(keyData==Keys.C)
        {
            RefreshControl();
            return true;
        }


        return base.ProcessCmdKey(ref msg, keyData);
    }
 groupBoxName.Controls.OfType<TextBox>().ToList().ForEach(t => t.Clear());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM