in the windows form application that using TableLayoutPanel we get all control with function
Control control in tableLayoutPanel1.Controls
is there a way to get the controls if i don't use TableLayoutPanel in my Form?
You could create an extension method like the following:
public static class ControlExtensions
{
public static IEnumerable<Control> GetAllControls(this Control containerControl)
{
var controls = Enumerable.Empty<Control>();
controls = controls.Concat(containerControl.Controls.Cast<Control>());
foreach (Control control in containerControl.Controls)
{
controls = controls.Concat(control.GetAllControls());
}
return controls;
}
}
And use it like this:
foreach (Control c in theForm.GetAllControls())
{
Debug.WriteLine(c.Name);
}
Note the the method GetAllControls can be used with any Control
, not only with Form
If controls are not placed into a TableLayoutPanel
, then they are likely to be in the main form itself. So ypu can loop through them like this:
foreach(Control control in this.Controls)
{
//do somthing with the controls
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.