C # quickly clears the contents of all text boxes

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.