.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Windows Form Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Count of Controls in Form using C#.Net

Posted By:Narayanan       Posted Date: October 31, 2012    Points: 40    Category: Windows Form Controls    URL: http://www.dotnetspark.com  

Today , we will discuss about the Count of Controls in Form.The following are code for getting no of controls in Form.
 

Method for getting no of controls in Form:

 

 public IEnumerable<Control> GetAll(Control control, Type type)

        {

            var controls = control.Controls.Cast<Control>();

            return controls.SelectMany(ctrl => GetAll(ctrl, type))

                                      .Concat(controls)

                                      .Where(c => c.GetType() == type);

        }


 call Method in the Form Loading event(where ever you want)

var c = GetAll(this, typeof(Button));

MessageBox.Show("Total Controls: " + c.Count());


Result:

Total Controls: (no of Buttons in the form)


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend