Is it possible to select text on a Windows form label?

52,152

Solution 1

Is it possible to select text on a Windows form label? - NO (At least no easy way without overriding Label.Paint method)

You can easily change a TextBox for this purpose.

TextBox1.Text = "Hello, Select Me";
TextBox1.ReadOnly = true;
TextBox1.BorderStyle = 0;
TextBox1.BackColor = this.BackColor;
TextBox1.TabStop = false;
TextBox1.Multiline = True; // If needed

Don't believe? here is an example for you.

enter image description here

Option 2 (If you just want to enable copy label text)

Double clicking on the label copies the text to clipboard. This is the default winforms Label functionality. You can add a toolTip control to improve the usability if you like.

enter image description here

Solution 2

Double clicking on a label will copy the text to the clipboard. This is now the default behavior of Windows Forms labels.

Solution 3

Use a TextBox with BorderStyle set to None and Readonly set to true and Backcolor to match that of the container.

Solution 4

No, it's not possible to select text on the Windows Form Label. You can instead use a read only textbox for this.

Solution 5

You will not be able to highlight part of the text on a label. However, you can use an image and set it to the Label.Image property if the text for these labels is static.

Share:
52,152
tunafish24
Author by

tunafish24

Updated on July 05, 2022

Comments

  • tunafish24
    tunafish24 almost 2 years

    Is it possible to highlight/select part of text in a Windows Form label control? I know its possible with RTFtextbox control but that using that control would be overkill as I need to create many instances of the label.

  • tunafish24
    tunafish24 over 12 years
    it'll be really tricky to implement, plus the text is dynamic - thats why the need to highlight some of it.
  • The Muffin Man
    The Muffin Man almost 11 years
    The issue you might run into is if you're setting the text of the texbox dynamically it will hide the text if it's wider than the textbox, unlike the label.
  • Hi-Angel
    Hi-Angel over 9 years
    @TheMuffinMan you have to set «WordWrap» property to «true».
  • John Kurtz
    John Kurtz almost 7 years
    If needed, you can also set Multiline = True to be able to change the height of the TextBox.
  • reakt
    reakt over 5 years
    Double clicking on a label will copy the text to the clipboard. This is now the default behavior of Windows Forms labels. There is no need to do this yourself.
  • Ryan
    Ryan about 5 years
    This is one of those instances where time has changed what the correct answer is, but there's not a good way to go about making the answer more known.
  • Ryan
    Ryan about 5 years
    While this is still a valid approach, I would recommend checking out @vmil's answer below (which is that the default functionality of labels is now that double clicking on a label copies the text).
  • sɐunıɔןɐqɐp
    sɐunıɔןɐqɐp over 4 years
    This is quite a hidden feature, most users wouldn't figure this out, they would most likely select try to select the text and hit Ctrl+C.
  • reakt
    reakt over 4 years
    @sɐunıɔןɐqɐp In this instance you'd want to notify the user of the functionality.
  • stenci
    stenci almost 3 years
    The code may need to be changed if "\n" was used to create new lines in the Label; use "\r\n" in the TextBox instead;