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:
Total Controls: (no of Buttons in the 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 controls = control.Controls.Cast<Control>();
return controls.SelectMany(ctrl => GetAll(ctrl, type))
.Concat(controls)
.Where(c => c.GetType() == type);
}
var c = GetAll(this, typeof(Button));
MessageBox.Show("Total Controls: " + c.Count());
Result:MessageBox.Show("Total Controls: " + c.Count());
Total Controls: (no of Buttons in the form)