ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

ClearControls

clean the controls on a form. Please send suggestions.

Source

public static void ClearControls(this System.Windows.Forms.Form form)
{
   foreach(var item in form.Controls)
   {
      if (item is TextBox)
         (item as TextBox).Text = "";
      
      if (item is ComboBox)
         (item as ComboBox).SelectedIndex = -1;

      //if (item is ...)
   }

}

Example

//In a button 'Reset' click event in a WinForm

this.ClearControls();

Author: Wallison R Santos

Submitted on: 22 jun. 2010

Language: C#

Type: System.Windows.Forms.Form

Views: 5573