How to clear all textbox of the windows form in c# and vb.net

 
You can clear all textbox controls of the windows form with the help of this code snippet:

[vb]

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ClearAllTextBox()
 End Sub
 
Private Sub ClearAllTextBox()
        Dim _ctrl As Control
        Dim _txt As TextBox
 
        For Each _ctrl In Me.Controls
            If (_ctrl.GetType() Is GetType(TextBox)) Then
                _txt = CType(_ctrl, TextBox)
                _txt.Text = ""
            End If
        Next
 End Sub

[c#]

 private void ClearAllTextBox()
    {
    	Control _ctrl = default(Control);
    	TextBox _txt = default(TextBox);
    	foreach ( _ctrl in this.Controls) 
         {
    	    if ((object.ReferenceEquals(_ctrl.GetType(), typeof(TextBox)))) 
              {
    			_txt = (TextBox)_ctrl;
    			_txt.Text = "";
    		}
     	}
   }

Author: Ankur

Have worked primarily in the domain of Calling, CRM and direct advertisers services. My technological forte is Microsoft Technologies especially Dot Net (Visual Studio 2003, 2005, 2008, 2010 and 2012) and Microsoft SQL Server 2000,2005 and 2008 R2. My Area of Expertise is in C#. Net, VB.Net, MS-SQL Server, ASP. Net, Silverlight, HTML, XML, Crystal Report, Active Reports, Infragistics, Component Art, ComponeOne, Lead Tools etc.

2 thoughts on “How to clear all textbox of the windows form in c# and vb.net”

  1. Private Sub ClearAllTextBox()
    Dim ctrl As Control
     Dim ctrl1 As Control
     Dim txt As TextBox
     
     For Each ctrl In Me.Controls
     
     If (ctrl.GetType() Is GetType(GroupBox)) Then
     For Each ctrl1 In ctrl.Controls
     If (ctrl1.GetType() Is GetType(TextBox)) Then
     txt = CType(ctrl1, TextBox)
     txt.Text = ""
     End If
     Next
     
     If (ctrl.GetType() Is GetType(TextBox)) Then
     txt = CType(ctrl, TextBox)
     txt.Text = ""
     End If
     End If
     Next
    END SUB

Comments are closed.