3

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?

DonBoitnott
  • 10,787
  • 6
  • 49
  • 68
user629926
  • 1,910
  • 2
  • 18
  • 43

2 Answers2

3

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);
   }
}
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
1

set the property Control.TabStop to true

rajeemcariazo
  • 2,476
  • 5
  • 36
  • 62
  • According to the msdn docs for [Label](http://msdn.microsoft.com/en-us/library/1dsccs1d.aspx), `The TabStop property is not relevant for the Label class, so setting TabStop to true has no effect.` – SwDevMan81 Aug 01 '13 at 15:24
  • I read the same thing but somehow it works, maybe in coalition with ControlStyle.Selectible. – user629926 Aug 01 '13 at 15:27
  • @user629926 Works? Which VS version are you using? In VS 2010 it does not work! – varocarbas Aug 01 '13 at 15:28
  • Yeah looks like it could be the combination, @Hans Passant posted a similar solution for panels [here](http://stackoverflow.com/a/3562449/95573) – SwDevMan81 Aug 01 '13 at 15:30
  • @SwDevMan81 I am afraid that this is not the same. The Panel control does have the TabStop property (you can see it within its properties in the design view) but the Label doesn't have it. Perhaps there might be a work-around (logically, am not sure) but this link refers to a different reality. – varocarbas Aug 01 '13 at 15:34
  • @varocarbas - I posted a working example. I am on VS 2008, so let me know if that works for 2010. Yeah the Panel example was just to show that it also included both `SetStyle` **and** `TabStop`. It appears to need both. – SwDevMan81 Aug 01 '13 at 15:36
  • @SwDevMan81 yeah, clearly this works. But I think that this is not the point: you are creating a custom label; the OP says that the in-built control works by affecting a property it does not have (I can write LabelWhatever.TabStop = True but does not have any effect). One thing is accepting your new answer (which is right) and another thing is accepting this answer which is not so right; or, at least, it should be updated: if you want to use the TabControl, you have to create your own label. – varocarbas Aug 01 '13 at 15:42
  • @varocarbas - Right, setting the `TabStop` of a label has no affect, as indicated by the msdn docs. The answer does need to be updated (or accept mine ;) ) – SwDevMan81 Aug 01 '13 at 15:48
  • 2
    @SwDevMan81 the OP has already accepted yours. It was the right one even in its preliminary version (just converting to TextBox), I did +1 it. Sorry for rajeem_cariazo but I guess that he also wants to be awarded for right answers and this one wasn't right. – varocarbas Aug 01 '13 at 15:51