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

.NET Clear all Textbox's in windows form

Hi

I have a bunch of textboxes that sit in 2 different GroupBox's that are in a tab control
I would like to be able to reset all textboxes and combo boxes

Thanks
0
prowebinteractiveinc
Asked:
prowebinteractiveinc
1 Solution
 
Julian HansenCommented:
Try this
foreach (Control control in this.Controls)
{
  if (control.GetType() == typeof(TextBox) ||
      control.GetType() == typeof(ComboBox))
  {
    control.Text = "";
  }
}

Open in new window

0
 
jmcmunnCommented:
That will not catch the controls inside the GroupPanels and TabControl.  You would have to do something like this method...call this method using ClearTextBoxes(this.Controls) and you're all set.

private void ClearTextBoxes( Control.ControlCollection controls )
      {
         foreach( Control control in controls )
         {
            if( control.Controls.Count > 0 )
               ClearTextBoxes( control.Controls );

            if( control.GetType() == typeof( TextBox ) ||
                control.GetType() == typeof( ComboBox ) )
            {
               control.Text = "";
            }
         }
      }
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Since the targets are in multiple containers, nested at different levels, you need a RECURSIVE algorithm to find them:
Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Reset(Me)
    End Sub

    Private Sub Reset(ByVal ctl As Control)
        For Each childCtl As Control In ctl.Controls
            If TypeOf childCtl Is TextBox Then
                CType(childCtl, TextBox).Clear()
            ElseIf TypeOf childCtl Is ComboBox Then
                CType(childCtl, ComboBox).SelectedItem = Nothing
            ElseIf childCtl.HasChildren Then
                Reset(childCtl)
            End If
        Next
    End Sub

End Class

Open in new window

0
 
RobRotterdamCommented:
A TabControl and a groupbox are controls themselves, with the first solution the textboxes on them won't be cleared.

I'd suggest you create an Utilities class in your application and in that class a static function:

public static clearTextboxes (object ClearObject) 
{
   foreach (Control control in ClearObject)
   {
     string ControlType = control.GetType().ToString();
      if (ControlType == "System.Windows.Forms.TextBox" || ControlType == "System.Windows.Forms.GroupBox"       
       {
             control.Text = ""; 
        }
      if (ControlType == "System.Windows.Forms.Panel" ||
          ControlType == "System.Windows.Forms.TabControl" ||
          ControlType == "System.Windows.Forms.GroupBox")
      {
           clearTextBoxes(control); 
      }
   }
}

Open in new window


On the forms you want to clear you create a button and in the on-click event you define:
ApplicationUtil.clearTextBoxes(this); 

Open in new window

0
 
prowebinteractiveincAuthor Commented:
worked like a charm, not even anything to edit
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

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