Is it possible to select text on a Windows form label?
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.
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.
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.
tunafish24
Updated on July 05, 2022Comments
-
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 over 12 yearsit'll be really tricky to implement, plus the text is dynamic - thats why the need to highlight some of it.
-
The Muffin Man almost 11 yearsThe 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 over 9 years@TheMuffinMan you have to set «WordWrap» property to «true».
-
John Kurtz almost 7 yearsIf needed, you can also set Multiline = True to be able to change the height of the TextBox.
-
reakt over 5 yearsDouble 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 about 5 yearsThis 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 about 5 yearsWhile 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 over 4 yearsThis 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 over 4 years@sɐunıɔןɐqɐp In this instance you'd want to notify the user of the functionality.
-
stenci almost 3 yearsThe code may need to be changed if "\n" was used to create new lines in the Label; use "\r\n" in the TextBox instead;