3

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?

4

2 回答 2

3

TextBox将其设为 a 、设置BorderStyleNone、设置BackColorControl并设置ReadOnly为可能更容易True。这应该给出一个标签的外观,但仍然允许它被标记为焦点。

更新看起来像 和 的组合SetStyle(ControlStyles.Selectable, true);TabStop = true;您可以使用 Tab 键使标签聚焦。下面是一个简单的例子,它显示了它的工作原理:

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);
   }
}
于 2013-08-01T15:19:59.523 回答
1

将属性设置Control.TabStop为 true

于 2013-08-01T15:15:19.370 回答