0

Is there a way to clear textboxes at once when they have been renamed as txtFName, txtMName etc... They may have variety of names beginning with txt. Is this possible? Something like

private void btnReset_Click(object sender, EventArgs e)
{
    txtFname.Clear();
    txtLName.Clear();
    txtUsername.Clear();
    txtPasswrd.Clear();
    /*So many textboxes to be cleared*/
}

to be replaced with

private void ClearTextboxes(object obj)
{
    /*codes to clear textboxes*/
}

and then we can call it in the button click event

private void btnReset_Click(object sender, EventArgs e)
{
    ClearTextboxes();
    txtFname.Focus();
}
Mostafiz
  • 7,243
  • 3
  • 28
  • 42
mahk_bmr
  • 15
  • 1
  • 6
  • @RB Its only a duplicate if he is using WPF. OP Are you using WPF or WinForms? – CathalMF May 06 '16 at 11:53
  • 2
    @CathalMF This question has been asked many times. Here is a WinForms equivalent: http://stackoverflow.com/questions/4811229/how-to-clear-the-text-of-all-textboxes-in-the-form. A quick search would have found the OP many other examples. – RB. May 06 '16 at 11:54
  • 1
    WPF? WinForms? WebForms? ASP.NET MVC? SilverLight? ...? – Uwe Keim May 06 '16 at 12:03
  • Forgot to mention. I am using a win form application. – mahk_bmr May 06 '16 at 12:30

3 Answers3

1

this method clear all textbox from WinForm

void ClearTextboxes(System.Windows.Forms.Control.ControlCollection ctrls)
    {
       foreach (Control ctrl in ctrls)
       {
          if (ctrl is TextBox)
              ((TextBox)ctrl).Text = string.Empty;
          ClearTextboxes(ctrl.Controls);
       }
    }

and you can call it

private void btnReset_Click(object sender, EventArgs e)
{
    ClearTextboxes();
    txtFname.Focus();
}
Mostafiz
  • 7,243
  • 3
  • 28
  • 42
  • In ClearInputs(ctrl.Controls); ............ClearInputs means what? – mahk_bmr May 06 '16 at 12:38
  • sorry it was a silly mistake `ClearInputs` will be `ClearTextboxes` the method name itself, its recursively clear controls, name was remain my version – Mostafiz May 06 '16 at 12:59
  • eidited now correct one – Mostafiz May 06 '16 at 13:01
  • Your ClearTextboxes() can't be called that way. It needs an argument: an object of class System.Windows.Forms.Control.ControlCollection. How do I initiate that? – Spero Jun 14 '19 at 09:59
0

You can iterate through all textbox in your VisualTree like this:

foreach (TextBox txtBx in FindVisualChildren<TextBox>(this))
{
    txtBx.Clear();
}

FindVisualChildren :

public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
    {
        if (depObj != null)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                if (child != null && child is T)
                {
                    yield return (T)child;
                }

                foreach (T childOfChild in FindVisualChildren<T>(child))
                {
                    yield return childOfChild;
                }
            }
        }
    }   

As an argument you can pass any parent it can be StackPanel, or Grid or whole window.

SteppingRazor
  • 1,222
  • 1
  • 9
  • 25
  • `VisualTreeHelper` assumes WPF. Is this WPF? – CodeCaster May 06 '16 at 12:03
  • I meant the question, not your answer. Also, [don't copy answers](http://stackoverflow.com/questions/974598/find-all-controls-in-wpf-window-by-type), or at least not without attribution. – CodeCaster May 06 '16 at 12:07
0

One way using event is:

using System;
using System.Windows.Forms;

namespace textBoxs
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Subscribe();
        }

        private event Action ClearAll;

        void Subscribe()
        {
            ClearAll += tbA.Clear;
            ClearAll += tbB.Clear;
            ClearAll += tbC.Clear;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ClearAll();
        }
    }
}