问题
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