0

I have seen a lot of answers on the web stating that a Label's text can't be selected/copied in the way that a TextBox's contents can, but what is the underlying reason that a Label's text can't be copied?

Windows itself can find text under the cursor position, so why can't the WinForm Label control?

Richard Ev
  • 52,939
  • 59
  • 191
  • 278
radders
  • 923
  • 8
  • 29

1 Answers1

7

In order for a user to select or copy a control's text, the control must allow you to set focus to it, either by clicking or tabing to the control.

A Label doesn't allow this, by design.

Label controls are typically used to provide descriptive text for a control. For example, you can use a Label to add descriptive text for a TextBox control to inform the user about the type of data expected in the control.

So while Labels and TextBoxes both inherit from System.Windows.Control they are different things, intended for different purposes. In the same way that oranges and apples are both fruit, but are different.

However, if you're creating an application and want to have something that looks like a label, but allows the user to select (but not edit) the text, then you can use a TextBox with the following properties set:

  • Backcolor = Control
  • ReadOnly = true
  • BorderStyle = none

As shown below...

A textbox that looks like a label

Alternatively, if you have an application and want to get text from something like a label, you can use the Win32 API function GetWindowText, if you know the handle to the window that contains the text. In a Win32 context a "window" means just about anything distinct that is on the screen, not just the windows that you can drag around with your mouse. WinForms is an abstraction on top of all this.

As for getting the handle to the window that is under the mouse cursor, see this question.

Community
  • 1
  • 1
Richard Ev
  • 52,939
  • 59
  • 191
  • 278
  • 1
    .....I had no idea that labels weren't selectable.... #MindBlown.... Btw, did you really fend off an elephant with a laptop? Brah, that's not how you use laptops.... – DotNetRussell Jul 14 '16 at 15:21
  • @AnthonyRussell - you know, I actually did! On holiday in Zambia, Africa a few years ago, we'd been warned there were some male adolescents in heat near where we were staying. One approached us from behind some trees one evening. I shouted at it and waved my laptop around, which did the trick and it backed off. Those things are biiiiiiig! – Richard Ev Jul 14 '16 at 15:26
  • 1
    What brand of laptop? – DotNetRussell Jul 14 '16 at 15:26
  • Company-supplied Dell. ;) I had it with me so that I could copy photos over from my camera. – Richard Ev Jul 14 '16 at 15:27
  • Probably a Mac fan. They always use nature names in their OS – DotNetRussell Jul 14 '16 at 15:28
  • You could also notice that a label have an Image property, the textbox doesn't. Label also support Mnemonic (ALT + ... To focus a control) but could not have focus, it will be moved to the next control in the TabOrder (which could be a Textbox by exemple). – Marco Guignard Jul 14 '16 at 16:09
  • 1
    Thanks Richard for the explanation. The GetWindowText API call is probably the one I recall from VB6 days, coupled with obtaining the Handle of the Window the mouse is in.... – radders Aug 08 '16 at 15:25