C # traverse Form Controls (original from http://www.liangshunet.com/ca/201403/286434593.htm ),

Source: Internet
Author: User

C # traverse Form Controls (original from http://www.liangshunet.com/ca/201403/286434593.htm ),

I. C # traverse Form Controls

It mainly traverses the Form Control (Controls). If the Form contains Panel, Button, and TextBox Controls, the traversal code is as follows:

/// <Summary> /// Winform C # traverse the Form Control /// </summary> /// <param name = "ctrlName"> Control name </param> public void ForeachFormControls (string ctrlName) {foreach (Control ctrl in this. controls) {if (ctrl is Panel) {// operation code ctrl. backColor = Color. aquamarine;} else if (ctrl is Button) {ctrl. foreColor = Color. royalBlue;} else if (ctrl is TextBox) {ctrl. text = null;} // you can specify if (ctrl. name. equals (ctrlName) {vctrl. name = string. empty ;}}}

The above Code only performs some random operations based on the control type and the name of the passed control. You can change it to the required operation as needed.

II. C # Winform traversal control-Traverse sub-controls in the Panel

The Traversal method is the same as the control in the traversal form, except that this must be changed to the Panel name. If the Panel control only contains the Button and TextBox sub-controls, the Code is as follows:

/// <Summary> /// C # traverse the sub-control /// </summary> /// <param name = "ctrlName"> Control name </param> public void foreachPanelControls (string ctrlName) {foreach (Control ctrl in panel1.Controls) {if (ctrl is Button) {if (ctrl. name. equals (ctrlName) ctrl. foreColor = Color. royalBlue; else ctrl. foreColor = Color. skyBlue;} else if (ctrl is TextBox) {if (ctrl. name. equals (ctrlName) ctrl. name = "Current Value"; else ctrl. text = null ;}}}

 

Related Article

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.