C # quickly clears the contents of all text boxes
How do I clear all the text box contents in a form? In two cases: (1) When all text boxes are top-level controls, that is, they are located directly in this. Controls (or Groupbox.controls), this is the time to go through the cleanup. (2) When the text box is not all at the top level, that part is contained within some container controls, such as GroupBox, because the control is placed strictly in the form. Some of the text boxes at this point are located in this. Controls (that is, the form control set), while others are in Groupbox.controls.
for (1), this can be written directly:
or for Groupbox1.controls/panel1. Controlsforeach (Control Ctrl in Controls) { if (Ctrl is TextBox) Ctrl. Text = "";}
Writing a few simple lines of code to emphasize a place where some text boxes are defined by the user as a control that inherits the TextBox, as written above, can be recognized, but written as: if (ctrl. GetType () = = typeof (TextBox), which is relatively strict, and custom text boxes are not recognized.
To solve the problem raised in (2), write a more versatile and versatile way to detect and empty these text boxes. Here's how:
private void Cleartext (Control ctrltop) {
if (ctrltop.gettype () = = typeof (TextBox)) ctrltop.text = ""; else { foreach (Control ctrl in Ctrltop.controls) { cleartext (ctrl);//Cyclic call } }}
Where needed, call cleartext directly (this) and you're done.
C # quickly clears the contents of all text boxes