Make label participate in control tabbing

岁酱吖の 提交于 2019-12-23 22:40:07

问题


I have custom control that inherits from Label and has ControlStyle.Selectable set to true.

The control receives focus when the user clicks on it, but won't if the user tabs from another control.

Even when I have a form filled by only that type of controls none of them recieve focus by tabbing.

How can I make my Label receive focus by tabbing?


回答1:


It might be easier just to make it a TextBox, set the BorderStyle to None, set the BackColor to Control and set ReadOnly to True. This should give the appearance of a label, but still allow it to be tabbed onto for focus.

Update It looks like with a combination of SetStyle(ControlStyles.Selectable, true); and TabStop = true;, you can get the Label to focus using the Tab key. Below is a simple example that shows it working:

public class SelectableLabel : Label
{
   public SelectableLabel()
   {
      SetStyle(ControlStyles.Selectable, true);
      TabStop = true;
   }

   protected override void OnEnter(EventArgs e)
   {
      BackColor = Color.Red;
      base.OnEnter(e);
   }

   protected override void OnLeave(EventArgs e)
   {
      BackColor = SystemColors.Control;
      base.OnLeave(e);
   }

   protected override void OnMouseDown(MouseEventArgs e)
   {
      this.Focus();
      base.OnMouseDown(e);
   }
}



回答2:


set the property Control.TabStop to true



来源:https://stackoverflow.com/questions/17997836/make-label-participate-in-control-tabbing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!