2013-08-01 25 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