2013-08-01 27 views
3

我有自定义控件,它继承自Label并且ControlStyle.Selectable设置为true使标签参与控制挂钩

控件在用户点击它时收到焦点,但如果用户从另一个控件中选择标签,控件将不会收到焦点。

即使我只有一种填充了这种类型的控件的表单,它们都没有通过Tab键获得焦点。

我该如何让我的Label通过tab键获得焦点?

+0

'Selectable'不是'TabStop'。不同的财产。 – DonBoitnott

回答

2

可能更容易只是为了让一个TextBox,设置BorderStyleNone,设置BackColorControl并设置ReadOnlyTrue。这应该给出标签的外观,但仍然可以将其标记为焦点。

更新它看起来像的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); 
    } 
} 
1

设置该属性Control.TabStop为true

+0

根据[Label](http://msdn.microsoft.com/en-us/library/1dsccs1d.aspx)的msdn文档,“TabStop属性与Label类无关,所以将TabStop设置为true没有效果。' – SwDevMan81

+0

我读了同样的东西,但它以某种方式工作,可能与ControlStyle.Selectible联合。 – user629926

+0

@ user629926是否有效?你使用哪个VS版本?在VS 2010中它不起作用! – varocarbas