• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 649
  • Last Modified:

Clear all texboxes on a winform c#

Hi all,

I have a peice of code to clear all textboxes on a winform. This works fine but when I have a form with a control on it who can contain controls itself it doesn't work.

Can anyone tell me how to expand this so that it checks textboxes in controls? Like a form with tabpages wich has groupbox with controls in it.
Below my code.

Greetings
public static void ClearAllTextbox(Form frm)
        {
          foreach (Control ctrl in frm.Controls)
          {
            if (ctrl is TextBox || ctrl is RichTextBox)
            {
              if (ctrl.Text.Length > 0)
              {
                ctrl.Text = string.Empty;
              }
            }
          }
        }

Open in new window

0
Pit76
Asked:
Pit76
2 Solutions
 
Fernando SotoRetiredCommented:
Hi Pit76;

Something like this will do what you need.

Fernando
private void button2_Click(object sender, EventArgs e)
{
    ClearAllTextbox(this);
}
 
public static void ClearAllTextbox(Control ctls)
{
    foreach (Control ctl in ctls.Controls)
    {
        if (ctl is TextBox || ctl is RichTextBox)
        {
            if (ctl.Text.Length > 0)
            {
                ctl.Text = string.Empty;
            }
        }
        else
        {
            ClearAllTextbox(ctl);
        }
    }
}

Open in new window

0
 
Sreedhar VengalaSr. Consultant - Business IntelligenceCommented:
Hi Pit76

it is possible to clear all them at once but you will have to write recursive function becaues some controls could be in sub-containers

something as below:
Screen Shot of form Attached:
using System;
using System.Windows.Forms;
 
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            foreach (Control control in this.Controls)
            {
                ClearContent(control);
            }
            
        }
        public void ClearContent(Control parent)
        {
            if(parent.HasChildren)
            {
                foreach (var control in parent.Controls)
                {
                    var control1 = control as Control;
                    if (control1 != null) if (control1.HasChildren) ClearContent(control1);
                    if (control1 is TextBox) control1.Text = string.Empty;
                }
            }
            else
            {
 
                if (parent is TextBox) parent.Text = string.Empty;
            }
 
            
        }
    }
}

Open in new window

ClearTextboxes.jpg
0
 
Pit76Author Commented:
Hi guys,
I've split the points cos both of your solutions work for me. Thx guys!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now