问题描述
我有从 Label
继承的自定义控件,并将 ControlStyle.Selectable
设置为 true
.
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.
即使我有一个仅由该类型控件填充的表单,它们也不会通过 Tab 键获得焦点.
Even when I have a form filled by only that type of controls none of them recieve focus by tabbing.
如何让我的 Label
通过 Tab 键获得焦点?
How can I make my Label
receive focus by tabbing?
推荐答案
将其设为 TextBox
,设置BorderStyle
为None
,设置>BackColor
为 Control
并将 ReadOnly
设置为 True
.这应该提供一个标签的外观,但仍然允许它被标记为焦点.
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 看起来像SetStyle(ControlStyles.Selectable, true);
和 TabStop = true;
的组合,你可以得到使用 Tab 键聚焦的标签.下面是一个简单的例子,展示了它的工作原理:
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);
}
}
这篇关于使标签参与控制制表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!