Could Label get a focus?

18,185

Solution 1

Yes there is a Focus() method on Label and yes it is absolutely right it works; but behave differently. let me try to explain

A Label can be associated with some one input control, for instance a label for a user name text field, so there is concept of Associated Control with the label. AssociatedControlID on msdn

So you can associate an input control with a label and whenever label is selected the control passed to the associated input control.

Example here click on Email or Password labels in login box and see what happened, similarly if you call focus method on the label the focus will passed to the associated control.

Solution 2

From the documentation:

A control can be selected and receive input focus if all the following are true: the Selectable value of ControlStyles is set to true, it is contained in another control, and all its parent controls are both visible and enabled.

Since a Label control is not selectable, it cannot receive input focus, even if it inherits a Focus() method from Control. Therefore, the answer is no.

Solution 3

It's easy to findo out if a control's ca get focus. Just read the

.CanFocus

property which is inherited from the Control class.

The Windows Forms controls in the following list are not selectable. Controls derived from these controls are also not selectable. (see MSDN documentation)

  • Panel
  • GroupBox
  • PictureBox
  • ProgressBar
  • Splitter
  • Label
  • LinkLabel (when there is no link present in the control)

Also:

The Focus method returns true if the control successfully received input focus. The control can have the input focus while not displaying any visual cues of having the focus. This behavior is primarily observed by the nonselectable controls listed below, or any controls derived from them.

A control can be selected and receive input focus if all the following are true: the Selectable value of ControlStyles is set to true, it is contained in another control, and all its parent controls are both visible and enabled.

If you need a Label-like control that you can focus, just use a TextBox and make it readonly. Set a few other properties (styles, not selectable etc.) and you're done.

Solution 4

You will see that there is a read only property called CanFocus on a label, if you have a look at this property while debugging you will see it is false.

Every control that inherits from Control has the focus method, but that does not mean that it can be focused.

Solution 5

Label does gets the focus but it escalates it to the input field specified in its "for" attribute. Like:

<label for="firstname">First Name</label><input type="text" name="firstname" />

In this scenario if you click on the label it will throw the focus to the input field "firstname" associated with it.

Share:
18,185
Frankie Drake
Author by

Frankie Drake

Updated on June 04, 2022

Comments

  • Frankie Drake
    Frankie Drake almost 2 years

    I have a one question on my university's test about C#. Could label get a focus? As I can see on MSDN site, all Controls can get a focus, but some of them aren't selectable. So it's seems to me that the right answer is "Label could get a focus, but couldn't be selected". Also Label has a Focus() method. Please, help me understand. Thanx.

  • Cody Gray
    Cody Gray about 13 years
    Yes, this is the correct answer. The presence of Focus or CanFocus properties is really irrelevant. The Windows API (which WinForms is based around) has a hard rule about which controls can receive the focus. A label (used for displaying static text) is one of those that can never get the focus. No exceptions.
  • Frankie Drake
    Frankie Drake about 13 years
    So label couldn't get a focus?
  • TBohnen.jnr
    TBohnen.jnr about 13 years
    No, the label itself can't get focus even though the method is exposed
  • Frankie Drake
    Frankie Drake about 13 years
    thanx a lot it's very clear. But I've not marked that answer was about .NET 2.0! So label hasn't this property there. And is it available just for ASP .NET? Anyway thank you very much.
  • Cody Gray
    Cody Gray about 13 years
    @Daria: For WinForms, the control with the next tab index will receive the focus when you try to set the focus to a Label control. So say, for example, that you have a label with a tab index of 0 and a textbox with a tab index of 1. When you try to set focus to the label, the textbox will automatically get the focus.
  • Hugh W
    Hugh W over 11 years
    For me the above code half works. If I click on the SelectableLabel it does gain focus, but it appears to be impossible to select the control by using the tab key -- it seems to be excluded from the tab order.
  • jm.
    jm. about 10 years
    I think this was a C# question, not an HTML question.