title: ArgumentException 'child' is not a child control of this parent
body: <p>I have a Windows Form with a <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.splitcontainer.aspx" rel="nofollow">SplitContainer</a>. In this <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.splitcontainer.aspx" rel="nofollow">SplitContainer</a> are other Panels.</p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.splitcontainer.panel1.aspx" rel="nofollow">Panel1</a> contains a custom control called <code>InputControl</code> and a <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.textbox.aspx" rel="nofollow">TextBox</a> called <code>Display</code>.</p>
  
  <p>To find out which item is currently displayed, I turned to the <a href="http://msdn.microsoft.com/en-us/library/1fz293fh.aspx" rel="nofollow">GetChildIndex Method</a> and used the Windows Form as the parent:</p>
  
  <pre><code>int inputIndex = parent.Controls.GetChildIndex(InputControl);
  int displayIndex = parent.Controls.GetChildIndex(Display);
  </code></pre>
  
  <p>When I was coding this, I was not sure if I could pass the Windows Form in as the parent, so I put a breakpoint afterwards to check that the two indexes were greater than -1:</p>
  
  <pre><code>      if ((-1 &lt; inputIndex) &amp;&amp; (-1 &lt; displayIndex)) {
  </code></pre>
  
  <p>However, before the first integer <code>inputIndex</code> was assigned a value, I was awarded the <a href="http://msdn.microsoft.com/en-us/library/system.argumentexception.aspx" rel="nofollow">ArgumentException</a> with this message for my efforts:</p>
  
  <blockquote>
    <p>'child' is not a child control of this parent.</p>
  </blockquote>
  
  <p>OK, I suppose these are not direct child controls of this parent.</p>
  
  <p>The <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.splitcontainer.aspx" rel="nofollow">SplitContainer</a>, on the other hand, certainly is a direct child control. So, should I search the <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.splitcontainer.aspx" rel="nofollow">SplitContainer</a> control?</p>
  
  <p>Technically, the controls I want the z-index of are in the <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.splitcontainer.panel1.aspx" rel="nofollow">Panel1</a> control, though. Does that mean my code has to look this ugly?</p>
  
  <pre><code>int inputIndex = splitControl1.Panel1.Controls.GetChildIndex(InputControl);
  int displayIndex = splitControl1.Panel1.Controls.GetChildIndex(Display);
  </code></pre>
  
  <p>This seem like a very unhelpful way of getting the z-indexes for my two (2) controls.</p>
  
  <p>Is there a simpler way to go about this other than writing my own code to keep track of which control is on top?</p>
  
  <p>I'd like some Microsoft method that returns the z-index of any control, whether that control be a direct child control or some sort of other descendant of the parent. The answer is awarded, first, to the person who knows this.</p>
  
  <p>If that doesn't exist, how would I write one without having to create recursive loops? In the absence of an existing Microsoft method, this would serve as awarded answer.</p>
  
  <p>If a recursive loop is necessary, points (and possibly answer) to best recursive loop.</p>