How to check type and clear textboxs in c#

This article show you how to check type of textbox and clear all textboxes in form by using form controls. I have a form with six textboxses and I want to clear all textbox in my form when I click on clear button.
Sample form
Sample form
 Let’s create a function to clear textbox.
Code :

public void clear(Form frm)
{
    //find all object in form
    foreach(Object txt in frm.Controls){
       //check if type of objects are TextBox
       if (txt is TextBox) {
          //declare a variable , text , as Textbox
          TextBox text = (Object)txt as TextBox;
          //clear all value in Textboxs
          text.Text = "";
        }
    }
}
  
Calling function clear in Clear Button 
private void btnClear_Click(object sender, EventArgs e)
{
    //calling clear function           
    clear(this);
    //this : is presented of the form
}

Done...hope you enjoy it...:) :) 
How to check type and clear textboxs in c# How to check type and clear textboxs in c# Reviewed by BeiLover on 5:27 PM Rating: 5

No comments:

Powered by Blogger.