WinForm ways to traverse all child controls of a form _c# tutorial

Source: Internet
Author: User

The example in this article describes how WinForm traverses all child controls of a form. Share to everyone for your reference, specific as follows:

<summary>///C # only traverse control child controls, do not traverse the sun control///when the control has child controls, you need to iterate recursively to list all the controls on the control///</summary>///<typeparam na Me= "T" > control type </typeparam>///<param name= "Control" > Controls to Traverse </param>///<param name= " Controlsname "> Control name </param>///<returns></returns> public static T getcontrol<t>
  Control, String controlsname) where T:control {if (control = null) return null;
  Control _control; for (int i = 0; i < control. Controls.Count; i++) {_control = control.
    Controls[i];
    if (_control = null) return null; if (_control.
    Name = = Controlsname && _control is T) return (t) _control; if (_control.
      HasChildren) {_control = getcontrol<t> (_control, controlsname);
    if (_control!= null) return (T) _control;
} return null; ///<summary>///Traverse form All child controls///</summary>///<typeparam name= "T" > control type </typeparam>///<par Am name= "form" > Form name </param>///<param name= "Controlsname" > Control name </param>///<returns></returns> public static T
  getcontrol<t> (Form form, String controlsname) where T:control {T _control = null; for (int i = 0; i < form. Controls.Count; i++) {_control = getcontrol<t> (form.
    Controls[i], controlsname);
  if (_control!= null) return _control;
return null;

 }

Read more about C # Interested readers can view the site topics: "WinForm Control Usage Summary", "C # form Operation Tips Summary", "C # Common control usage Tutorial", "C # Programming Thread usage Tips", "C # Operation Excel Skills Summary", "C # Summary of operational skills in XML files, C # tutorial on data structure and algorithms, C # array operation techniques Summary, and C # Introduction to object-oriented Programming

I hope this article will help you with C # programming.

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.