Andy
Andy

Reputation: 407

Draw border when the Control is focused

I want to draw a border on any Control that has focus, the border has to disappear when the control no longer has focus. I have tried to below code to draw a border, but I have no idea how I can let the border be painted before it disappears.

void mButton_Paint(object sender, PaintEventArgs e) {
    ControlPaint.DrawBorder(e.Graphics, ((Control)sender).ClientRectangle, Color.DarkBlue, ButtonBorderStyle.Solid);
}

Upvotes: 2

Views: 1851

Answers (1)

RenniePet
RenniePet

Reputation: 11658

Try this. (Turned out to be a lot messier than I expected.)

   public partial class FormSO29381768 : Form
   {
      // Constructor
      public FormSO29381768()
      {
         InitializeComponent();

         InstallEventHandlers(this);
      }


      /// <summary>
      /// Recursive method to install the paint event handler for all container controls on a form, 
      /// including the form itself, and also to install the "enter" event handler for all controls 
      /// on a form.
      /// </summary>
      private void InstallEventHandlers(Control containerControl)
      {
         containerControl.Paint -= Control_Paint;  // Defensive programming
         containerControl.Paint += Control_Paint;

         foreach (Control nestedControl in containerControl.Controls)
         {
            nestedControl.Enter -= Control_ReceivedFocus;  // Defensive programming
            nestedControl.Enter += Control_ReceivedFocus;

            if (nestedControl is ScrollableControl)
               InstallEventHandlers(nestedControl);
         }
      }


      /// <summary>
      /// Event handler method that gets called when a control receives focus. This just indicates 
      /// that the whole form needs to be redrawn. (This is a bit inefficient, but will presumably 
      /// only be noticeable if there are many, many controls on the form.)
      /// </summary>
      private void Control_ReceivedFocus(object sender, EventArgs e)
      {
         this.Refresh();
      }


      /// <summary>
      /// Event handler method to draw a dark blue rectangle around a control if it has focus, and 
      /// if it is in the container control that is invoking this method.
      /// </summary>
      private void Control_Paint(object sender, PaintEventArgs e)
      {
         Control activeControl = this.ActiveControl;
         if (activeControl != null && activeControl.Parent == sender) 
         {
            e.Graphics.DrawRectangle(Pens.DarkBlue, 
                        new Rectangle(activeControl.Location.X - 2, activeControl.Location.Y - 2, 
                                      activeControl.Size.Width + 4, activeControl.Size.Height + 4));
         }
      }
   }

Upvotes: 2

Related Questions