title: How to loop through all controls in a Windows Forms form or how to find if a particular control is a container control?
body: <p>I will tell my requirement. I need to have a <code>keydown</code> event for each control in the <a href="http://en.wikipedia.org/wiki/Windows_Forms" rel="noreferrer">Windows Forms</a> form. It's better to do so rather than manually doing it for all controls if what I have to do for all keydown events is the same.</p>
  <p>So I could basically do this:</p>
  <pre><code>foreach (Control c in this.Controls)
      c.KeyDown+= new KeyEventHandler(c_KeyDown);
  </code></pre>
  <p>But here, the foreach doesn't loop inside those controls which reside inside a groupBox or a tabControl. I mean if the form (this) contains a groupBox or some other container control, then I can get a keydown event for that particular container control. And the <code>foreach</code> doesn't loop through controls that reside inside that container control.</p>
  <p>Question 1: How do I get a keydown event for &quot;all&quot; the controls in a form?</p>
  <p>If the above puzzle is solved, then my problem is over.</p>
  <p>This is what I can otherwise do:</p>
  <h3>Mainly pseudo code</h3>
  <pre><code>foreach (Control c in this.Controls)
  {
       c.KeyDown += new KeyEventHandler(c_KeyDown);
  
       if (c is Container control)
             FunctionWhichGeneratesKeyDownForAllItsChildControls(c)
  }
  </code></pre>
  <p>I know I will have to go through <code>FunctionWhichGeneratesKeyDownForAllItsChildControls(c)</code> many times over to get keydown for all controls if there are groupboxes inside a groupbox or so. I can do it. My question is,</p>
  <p>Question 2: How do I check if <code>c</code> is a container control?</p>