c# - Draw border when the Control is focused -


i want draw border on control has focus, border has disappear when control no longer has focus. have tried below code draw border, have no idea how can let border painted before disappears.

void mbutton_paint(object sender, painteventargs e) {     controlpaint.drawborder(e.graphics, ((control)sender).clientrectangle, color.darkblue, buttonborderstyle.solid); } 

try this. (turned out lot messier expected.)

   public partial class formso29381768 : form    {       // constructor       public formso29381768()       {          initializecomponent();           installeventhandlers(this);       }         /// <summary>       /// recursive method install paint event handler container controls on form,        /// including form itself, , install "enter" event handler controls        /// on 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 scrollablecontrol)                installeventhandlers(nestedcontrol);          }       }         /// <summary>       /// event handler method gets called when control receives focus. indicates        /// whole form needs redrawn. (this bit inefficient, presumably        /// noticeable if there many, many controls on form.)       /// </summary>       private void control_receivedfocus(object sender, eventargs e)       {          this.refresh();       }         /// <summary>       /// event handler method draw dark blue rectangle around control if has focus, ,        /// if in container control invoking 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));          }       }    } 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -