我有自定义控件,它继承自Label
并且ControlStyle.Selectable
设置为true
。使标签参与控制挂钩
控件在用户点击它时收到焦点,但如果用户从另一个控件中选择标签,控件将不会收到焦点。
即使我只有一种填充了这种类型的控件的表单,它们都没有通过Tab键获得焦点。
我该如何让我的Label
通过tab键获得焦点?
我有自定义控件,它继承自Label
并且ControlStyle.Selectable
设置为true
。使标签参与控制挂钩
控件在用户点击它时收到焦点,但如果用户从另一个控件中选择标签,控件将不会收到焦点。
即使我只有一种填充了这种类型的控件的表单,它们都没有通过Tab键获得焦点。
我该如何让我的Label
通过tab键获得焦点?
可能更容易只是为了让一个TextBox
,设置BorderStyle
到None
,设置BackColor
到Control
并设置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);
}
}
设置该属性Control.TabStop
为true
根据[Label](http://msdn.microsoft.com/en-us/library/1dsccs1d.aspx)的msdn文档,“TabStop属性与Label类无关,所以将TabStop设置为true没有效果。' – SwDevMan81
我读了同样的东西,但它以某种方式工作,可能与ControlStyle.Selectible联合。 – user629926
@ user629926是否有效?你使用哪个VS版本?在VS 2010中它不起作用! – varocarbas
'Selectable'不是'TabStop'。不同的财产。 – DonBoitnott