72

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.

CharithJ
  • 46,289
  • 20
  • 116
  • 131
tunafish24
  • 2,288
  • 6
  • 28
  • 47
  • 1
    What do you mean by "select"? You mean select it by code, to use this text after? – GianT971 Oct 13 '11 at 00:33
  • 2
    sorry @ John...by select I mean text-selection e.g. in RichTextBox you can use "SelectedText" etc properties to select/highlight a portion of the entire text. The effect is similar to selecting/highlighting text manually with mouse. – tunafish24 Oct 13 '11 at 00:39
  • 2
    Just an idea: you could create a class derived from Label in which you have some property to store starting and ending index of text to be selected, then overriding onPaint event you can highlight text drawing a semitransparent rectangle over it – Marco Oct 13 '11 at 00:41

7 Answers7

117

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

CharithJ
  • 46,289
  • 20
  • 116
  • 131
  • 7
    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. – The Muffin Man May 31 '13 at 21:07
  • 3
    @TheMuffinMan you have to set «WordWrap» property to «true». – Hi-Angel Jan 28 '15 at 12:09
  • 1
    If needed, you can also set Multiline = True to be able to change the height of the TextBox. – John Kurtz Jul 06 '17 at 16:22
  • 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). – Ryan May 02 '19 at 18:12
  • 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; – stenci Jun 03 '21 at 18:49
22

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

reakt
  • 500
  • 8
  • 25
  • 5
    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 May 02 '19 at 18:10
  • 2
    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. – sɐunıɔןɐqɐp Sep 03 '19 at 06:44
  • 2
    @sɐunıɔןɐqɐp In this instance you'd want to notify the user of the functionality. – reakt Nov 13 '19 at 20:13
16

Like Bala R answered:

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

If the Text string is very long, and the Width of the TextBox is not enough to display all text, then you can set the Width property of the TextBox to display all it's Text.

If you need to know the correct number for Width, then you can use the MeasureString method of Graphics for this. You can get the instance from CreateGraphics() method of the Control (TextBox in this case).

First parameter is TextBox's Text, and second parameter is TextBox's Font. This function returns SizeF struct. You need only the Width property of it, convert it to integer with (int)size.Width or (int)Math.Round(size.Width).

Don't forget to call the Dispose() method of the graphics instance after, because you won't need it anymore.


You can write your own function that will do all this process:

static void SetText(TextBox textBox, string str)
{
   Graphics graphics = textBox.CreateGraphics();
   SizeF size = graphics.MeasureString(str, textBox.Font);
   graphics.Dispose();
   textBox.Width = (int)Math.Round(size.Width);
   textBox.Text = str;
}
Lorenzo Belfanti
  • 1,205
  • 3
  • 25
  • 51
  • 2
    If you have a fixed-width text box that you want to size the height of with this code, see this Question: https://stackoverflow.com/questions/29299297/measure-text-height-wrapped-in-a-specified-width – QuickDanger Sep 07 '17 at 17:43
8

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

Bala R
  • 107,317
  • 23
  • 199
  • 210
7

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

Amry
  • 4,791
  • 2
  • 23
  • 24
3

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.

gjohn
  • 132
  • 4
  • 1
    it'll be really tricky to implement, plus the text is dynamic - thats why the need to highlight some of it. – tunafish24 Oct 13 '11 at 02:11
0

I know this question is about selecting parts of the text of a label but I assume the text shall ultimately be placed on the clipboard.

So if you don't mind copying the whole text, just set a Click event on the label to copy its text to the clipboard:

myLabel.Click += new System.EventHandler(MyLabel_Click);

// ...

private void MyLabel_Click(object sender, EventArgs e)
{
   Clipboard.SetText(myLabel.Text);
}
Markus L
  • 932
  • 2
  • 20
  • 38
  • 2
    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. – reakt Feb 19 '19 at 21:44