title: Draw border around a Control on Button_Click
body: <p>When a user clicks my Validate Button (<em>in my C#, WinForm, .net 3.5 app</em>) I would like to draw a border around a certain control if it is empty. Say a textbox that is named tbxLastName I thought I needed to do something like this --> </p>
  
  <pre><code>ControlPaint.DrawBorder(Graphics.FromHwnd(this.Handle), 
      tbxLastName.ClientRectangle, Color.Firebrick, ButtonBorderStyle.Solid);
  </code></pre>
  
  <p>Unfortunately, I have no idea what to put for the Graphics Object as what I have does NOTHING.  </p>
  
  <p>All of the examples I have come across, <a href="http://msdn.microsoft.com/en-us/library/h85zz274.aspx" rel="nofollow noreferrer">MSDN - HERE</a>, have this code in a Paint Event. Like so --></p>
  
  <pre><code>private void panel1_Paint(object sender, PaintEventArgs e)
  {    
      ControlPaint.DrawBorder(e.Graphics, this.panel1.ClientRectangle, 
          Color.DarkBlue, ButtonBorderStyle.Solid);
  }
  </code></pre>
  
  <p>I, however, only want to have the border appear while certain conditions are meet which is kicked off by a Button_Click</p>
  
  <hr>
  
  <p>So many of the suggestions suggest using a container object to hold the textbox and call it's Paint_Event.  I did this and a box appears but NOT around the control.  It appears in the Top Left corner of the Container Control.  Here is what I am doing --></p>
  
  <pre><code>    private void grpImmunizationCntrl_Paint(object sender, PaintEventArgs e)
      {
          if (lkuNOImmunizationReason.Text.Equals(string.Empty)
          {
             ControlPaint.DrawBorder(
                      e.Graphics, lkuNOImmunizationReason.ClientRectangle,
                          Color.Firebrick, ButtonBorderStyle.Solid);
          }
      }
  </code></pre>
  
  <hr>
  
  <p>EDIT</p>
  
  <p>This is what I came up with combining suggestions here with what worked for me.</p>
  
  <pre><code>    public static void HighlightRequiredFields(Control container, Graphics graphics, Boolean isVisible)
      {
          Rectangle rect = default(Rectangle);
          foreach (Control control in container.Controls)
          {
              if (control.Tag is string &amp;&amp; control.Tag.ToString() == "required")
              {
                  rect = control.Bounds;
                  rect.Inflate(3, 3);
                  if (isVisible &amp;&amp; control.Text.Equals(string.Empty))
                  {
                      ControlPaint.DrawBorder(graphics, rect, Color.FromArgb(173,216,230), ButtonBorderStyle.Solid);
                  }
                  else
                  {
                      ControlPaint.DrawBorder(graphics, rect, container.BackColor, ButtonBorderStyle.None);
                  }
              }
  
              if (control.HasChildren)
              {
                  foreach (Control ctrl in control.Controls)
                  {
                      HighlightRequiredFields(ctrl, graphics, isVisible);
                  }
              }
          }
      }
  </code></pre>
  
  <p>I call this from the <code>Paint_Event</code> of any Container I need to.</p>