4
 public void clear()
  {
    lblage.Text = "";
    lblclosingbirds.Text = "";
    lbltypeoffeed.Text = "";
    lbltypeoffeedf.Text = "";
    lblstdfeed.Text = "";
    lblstdfeedf.Text = "";
    lblstdhd.Text = "";
    lblstdhe.Text = "";
    lblExpeggs.Text = "";
    lblbirdsf.Text = "";
    txtacteggs.Text = "";
    txtactfeed.Text = "";
    txtactfeedf.Text = "";      
    txtfemaleclosingstock50.Text = "";
    txtfemaleclosingstock70.Text = "";
    txtmale50kgcstock.Text = "";
    txtmale70kgscstock.Text = "";
    txtmort.Text = "";
    txtmortf.Text = "";
    txtuseeggs.Text = "";
    ddlFemaleFeedtype.SelectedValue = "0";
    ddlMaleFeedtype.SelectedValue = "0";

}

how to use foreach loop method for replace with clear()..please tell me.. any one... is it possible to write foreach loop...please tell me

Sambasiva
  • 1,034
  • 3
  • 16
  • 29

4 Answers4

6
void ClearAllControlsRecursive(Control container)
{
    foreach(var control As Control in container.Controls)
    {
        if(typeof control is TextBox)
        {
            ((TextBox)control).Text = String.Empty;
        }
        if(control.HasControls())
        {
            ClearAllControlsRecursive(control);  
        }
    }
}

then call this method like this:

ClearAllControlsRecursive(yourContainer);

You can also switch on the control type and clear the values accordingly.

Superzadeh
  • 1,106
  • 7
  • 23
  • foreach(var control As Control in (pnl.HasControls())) {} – Sambasiva Oct 03 '13 at 09:13
  • Nope, you need to put the for loop in a method, that takes in parameter a container. Then, you can make it recursive. I edited my Answer with an example. – Superzadeh Oct 03 '13 at 09:22
  • Then you should ask another question on this site about it. But before you do that, make sure you actually try to debug and code it. This is not a "do my homework" site. – Superzadeh Oct 03 '13 at 09:33
  • Updated the code, Controls is a Property, not a method. Just remove the "()" and it will work ... – Superzadeh Oct 03 '13 at 09:37
2
void ClearAllControlsRecursive(Control container)
{
    foreach (var control in container.Controls)
    {



        if (control is TextBox)
        {
            ((TextBox)control).Text = String.Empty;
        }


    }
}

and i cal this method like...

ClearAllControlsRecursive(panel1);
Sambasiva
  • 1,034
  • 3
  • 16
  • 29
1

you can try like this

foreach (object control in form1.Controls) {
    if (control is TextBox) {
        control.Text = "";
    }
}
शेखर
  • 17,412
  • 13
  • 61
  • 117
0

you can use this extension method :

public static IEnumerable<TControl> GetChildControls(this Control control) where TControl : Control
    {
        var children = (control.Controls != null) ? control.Controls.OfType<TControl>() : Enumerable.Empty<TControl>();
        return children.SelectMany(c => GetChildControls(c)).Concat(children);
    }

use :

foreach (TextBox tb in this.GetChildControls<TextBox>())
{
    tb.Text = String.Empty;
}

and here is a code(answer the question) for clear all controls on the form.

Another method :

yourForm.Controls.OfType<TextBox>().ToList().ForEach(textBox => textBox.Clear());
Community
  • 1
  • 1
Sirwan Afifi
  • 10,654
  • 14
  • 63
  • 110