J
Joe S.
I need to find all instances of a specific type of control on a Windows
Form -- for example all DataGrids. The code I came up with is this:
foreach(Control ctrl in this.Controls)
{
if(ctrl.GetType() == Type.GetType("System.Windows.Forms.DataGrid"))
Console.WriteLine(((DataGrid)ctrl).Name);
}
This code has two problems. Something's wrong with the type checking,
because it doesn't find any DataGrids on a form.
Even if it did work, the second problem is that this would find only
DataGrids put directly on the form (in the Form.Controls collection), but a
Grid can also be in the ControlCollection of a GroupBox or some other
container. How to loop through all controls in a form regardless of their
parent control?
Best regards,
Joe
Form -- for example all DataGrids. The code I came up with is this:
foreach(Control ctrl in this.Controls)
{
if(ctrl.GetType() == Type.GetType("System.Windows.Forms.DataGrid"))
Console.WriteLine(((DataGrid)ctrl).Name);
}
This code has two problems. Something's wrong with the type checking,
because it doesn't find any DataGrids on a form.
Even if it did work, the second problem is that this would find only
DataGrids put directly on the form (in the Form.Controls collection), but a
Grid can also be in the ControlCollection of a GroupBox or some other
container. How to loop through all controls in a form regardless of their
parent control?
Best regards,
Joe