Finding all controls of specified type on a form

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Joe S. (dontmail_at_nowhere.com)
Date: 08/08/04


Date: Sun, 8 Aug 2004 23:36:04 +0200

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



Relevant Pages

  • DataGrid autosized larger than needed when large font selected.
    ... the control is started from an ActiveX control (which is how our software ... the larger DataGrid spacing is used. ... there appears to be a difference in the final spacing and layout of DataGrids ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: New to Threading
    ... > problem creating a thread, addressing it to a Sub Procedure, and ... > boxes and datagrids go blank or Visibility = False. ... > if I specifically set the visibility to True after the thread. ... A control must only be accessed from the thread that created the control. ...
    (microsoft.public.dotnet.languages.vb)
  • Wandering Controls - control location changes in designer
    ... containing various controls (mostly label, text boxes, check boxes, date ... pickers, combo boxes and datagrids). ... all) control location changes when switching to design view after modifying ... If I do not move the control back in the designer, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: EnableViewState
    ... you can enableviewstate on a control by control basis (the ... > pages enableviewstate must be set for true though or all the controls on ... Yes, as a rule I bind the datagrids only once on_load, and I always use ... > as though viewstate was enabled. ...
    (microsoft.public.dotnet.framework.aspnet)
  • log on and administrative account
    ... For your second problem, try going into you control panel/ ... user accounts/ changing the type of account. ... >click on an icon to begin, there is no icon on the page, ...
    (microsoft.public.windowsxp.security_admin)