How to delete all data from controls in opened forms using C# ???
I will separate this problem into two tasks :
I- Delete all data from one form ( main form )
II- Reaching to other open forms and delete their data .
but before starting , first create a form contain
some combo box ,text box , labels and one button
I- Delete all data from one form ( main form ) :
insert this code in the button you created ( clear button )
private void Clear_Click(object sender, EventArgs e) foreach (Control ctrl in cc) case "System.Windows.Forms.Label": |
II- Reaching other open forms and delete their data :
its the easy part , just replace one line code and add a for loop .
replace this line :
cc = this.Controls;
with this line :
cc = Application.OpenForms[i].Controls;
* Application.openforms[i] to reach to all open forms
private void Clear_Click(object sender, EventArgs e) Control.ControlCollection cc; //cc = this.Controls; // previous code |
1 comment:
thank you mohamed nabil for your hint
, you can replace this line :
string type = ctrl.GetType().ToString();
and switch statement with :
If ( ctrl is TextBox )
{
// some action
}
Post a Comment