title: How do I reference and act on parent and child containers
body: <p>Say what you want about JavaScript, but navigating hierarchy is a breeze. Maybe C# is too, but I don't know how. I have a program with a series of controls where the control is repeated over and over:</p>
  <p><a href="https://i.stack.imgur.com/9swC4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9swC4.png" alt="form buttons example pic" /></a></p>
  <p>The buttons all operate the same so I'm repeating a lot of code and it seems inefficient:</p>
  <pre class="lang-cs prettyprint-override"><code>        // There's a function just like this for EVERY button
          private void FixedF1(bool isFixed)
          {
              if (isFixed)
              {
                  F1FixerBtnDot.ForeColor = Color.FromArgb(80, 214, 0);
                  F1FixerBtnDot.TextAlign = ContentAlignment.TopLeft;
                  F1FixerBtnArea.BackgroundImage = Properties.Resources.green_btn_bk_sm;
                  F1FixerBtnBox.Text = &quot;Fixed!&quot;;
                  PrefMatch(&quot;KillF1Help&quot;, &quot;yes&quot;, F1FixerBtnDot);
              }
              else
              {
                  F1FixerBtnDot.ForeColor = Color.FromArgb(244, 180, 0);
                  F1FixerBtnDot.TextAlign = ContentAlignment.TopRight;
                  F1FixerBtnArea.BackgroundImage = Properties.Resources.yellow_btn_bk_sm;
                  F1FixerBtnBox.Text = &quot;Fix it!&quot;;
                  PrefMatch(&quot;KillF1Help&quot;, &quot;no&quot;, F1FixerBtnDot);
              }
  
          }
  
          private void F1FixerBtnDot_Click(object sender, EventArgs e)
          {
              if (regStuff.F1HelpActive())
              {
                  // Save the new setting
                  prefs.SetPref(&quot;KillF1Help&quot;, &quot;no&quot;);
                  // Toggle it
                  regStuff.RestoreF1();
                  FixedF1(false);
              }
              else
              {
                  prefs.SetPref(&quot;KillF1Help&quot;, &quot;yes&quot;);
                  regStuff.KillF1();
                  FixedF1(true);
              }
          }
  </code></pre>
  <p>In the first function, I have code that turns the button &quot;on&quot; and &quot;off&quot;. The operation is basically repeated for each of these buttons and I might have 30 or more. I'm trying to recode it something like this (based on how I would do this in JS):</p>
  <pre class="lang-cs prettyprint-override"><code>        private void ToggleFixedButton(GroupBox which, bool isFixed, string prefName)
          {
              which.child(islabel).ForeColor = isFixed ? Color.FromArgb(80, 214, 0) : Color.FromArgb(244, 180, 0);
              which.child(islabel).TextAlign = isFixed ? ContentAlignment.TopLeft : ContentAlignment.TopRight;
              which.child(isPanel).BackgroundImage = isFixed ? Properties.Resources.green_btn_bk_sm : Properties.Resources.yellow_btn_bk_sm;
              which.Text = isFixed ? &quot;Fixed!&quot; : &quot;Fix it!&quot;;
              PrefMatch(&quot;KillF1Help&quot;, isFixed ? &quot;yes&quot; : &quot;no&quot;, which.child(islabel));
          }
  
          private void F1FixerBtnDot_Click(object sender, EventArgs e)
          {
              if (regStuff.F1HelpActive())
              {
                  prefs.SetPref(&quot;KillF1Help&quot;, &quot;no&quot;);
                  regStuff.RestoreF1();
                  ToggleFixedButton(sender.Parent, false, &quot;KillF1Help&quot;);
              }
              else
              {
                  prefs.SetPref(&quot;KillF1Help&quot;, &quot;yes&quot;);
                  regStuff.KillF1();
                  ToggleFixedButton(sender.Parent, true, &quot;KillF1Help&quot;);
              }
          }
  </code></pre>
  <p>Bottom line, it doesn't seem smart to have a separate function that does the same thing for 30 controls when I should be able to do it with ONE assuming I can figure out how to send it the information it needs to access the form elements involved.</p>