c# - TabStop and Focus on a GroupBox -


i have created custom control extends groupbox. control supports collapsing , expanding , use groupboxrenderer , buttonrenderer make typical groupbox has button in corner. have handled appropriate mouse events make "button" behave , regular button. have hit problem groupbox not receive focus using tabstop. there anyway can collapsable groupbox receive focus tabstop?

i hoping use trick how set focus control after validation in .net set focus in enter event haven't come way of determining when should focus. devise way of finding siblings next highest , lowest tabindex (or childindex if same tabindex) , determine if lost focus seems bit hacky , high chance of breaking if don't right.

note: did create user control not wanted various reasons including:

  1. it not control contains button , groupbox (it happens sort of way), groupbox
  2. flexibility
  3. coupling between backend code , ui
  4. dynamic layout
  5. shared across many projects require toolbox support , customising ui , layout of entire control

here looks when expanded: enter image description here

and when has been collapsed (and has focus): enter image description here

checkgroup => here approach custom control inherits control , uses groupboxrenderer , checkboxrenderer in onpaint method. container mimics having focus changing how checkboxrenderer draws. code collapses checkgroup , disables child controls, remove either or both desired.

using system; using system.componentmodel; using system.drawing; using system.text; using system.windows.forms; using system.windows.forms.visualstyles; namespace coolcontrols {  [designer("system.windows.forms.design.parentcontroldesigner, system.design", typeof(system.componentmodel.design.idesigner))]  public partial class checkgroup : control {     public event eventhandler checkboxgotfocus;     public event eventhandler checkboxlostfocus;      private int _checkboxsidelength;     private rectangle _checkboxborderrectangle;     private bool _focused = false;     private bool _checked;     private checkboxstate _checkedstate = checkboxstate.uncheckednormal;     private int _expandedheight;      [category("behavior")]     [description("get or set whether checkbox checked.")]     public bool checked     {         { return _checked; }         set         {             setcheckedstate(value);         }     }      public checkgroup()     {         initializecomponent();         initcontrol();     }      private void initcontrol()     {         _checkboxsidelength = 15;         _checked = true;         _focused = false;         _checkboxborderrectangle = new rectangle(0, 0, _checkboxsidelength - 1, _checkboxsidelength - 1);     }      private void setcheckedstate(bool ptochecked)     {         _checked = ptochecked;         if (_checked)         {             _checkedstate = checkboxstate.checkednormal;             this.height = _expandedheight;         }         else         {             _checkedstate = checkboxstate.uncheckednormal;             this.height = _checkboxsidelength;         }         foreach (control c in this.controls)         {             c.enabled = _checked;         }         this.invalidate();     }      protected override void onpaint(painteventargs pe)     {         base.onpaint(pe);         graphics g = pe.graphics;         groupboxrenderer.drawgroupbox(g, clientrectangle, "   " + this.text, this.font, this.forecolor, textformatflags.left, groupboxstate.normal);         checkboxrenderer.drawcheckbox(g, clientrectangle.location, _checkboxborderrectangle, "", null, textformatflags.left, _focused, _checkedstate);     }      protected override void onsizechanged(eventargs e)     {         base.onsizechanged(e);         if (_checked)         {             _expandedheight = this.size.height;         }     }      protected override void onmousedown(mouseeventargs e)     {         base.onmousedown(e);         if (e.location.y <= _checkboxsidelength)         {             setcheckedstate(!_checked);         }     }      protected override void ongotfocus(eventargs e)     {         base.ongotfocus(e);         _focused = true;         invalidate();         checkboxgotfocus.invoke(this, new eventargs());     }      protected override void onlostfocus(eventargs e)     {         base.onlostfocus(e);         _focused = false;         invalidate();         checkboxlostfocus.invoke(this, new eventargs());     }      protected override void onkeydown(keyeventargs e)     {         base.onkeydown(e);         if (e.keycode == keys.space)         {             setcheckedstate(!_checked);         }     }      protected override void onresize(eventargs e)     {         base.onresize(e);         invalidate();     } } } 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -