Windows Form

C# Loop through Controls on a Form

Often you want to address several controls at once, for example, all labels that have the names of Label0 – Label 19.
This can be done by accessing the this.Controls collection in a loop.

Here’s an example:

This works as long as all controls are in one container. If you are in different containers (panel, splitconteiner, etc.), you have to specify this control list. eg this.Panel1.Controls

Or you can use another an example is via foreach statement, when you click the program’s Button button, the following code executes.

 

 

 

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.