• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7358
  • Last Modified:

C# Is it possible to make the text in a label selectable to the clipboard?

Is it possible to select the text with a mouse within a label and then right click and copy it to a clipboard? I read somewhere else ( Granted this was VB .NET and not C#) that you set Locked = true and Style = Flat and that would allow you to do it. Since the C# label does not have a direct Style property i tired this with the flatstyle property instead. Thanks for the help folks
1 Solution
no, i don't think you can do it with the label... what you can do is:

1. add a ContextMenu to your project
2. add one menu item to the menu above and set the text to Copy
3. add this menu as a context menu of your label (say label1)
4. double click the "Copy" menu item and add the following line:

          Clipboard.SetDataObject( label1.Text, true );

Now it will copy any text from your label and will paste it anywhere else (like in some word document or text document) with the Paste command or Ctrl+V

Good luck,
i agree with Yurich; it isn't possible in the common terms of selecting and copying.  to add to Yurich's possible solutions, you could also use a textbox, dimensioned to fit the text exactly, and then set borderstyle = flat, and probably backcolor= control to get a 'label lookalike' that allows selection and copy.  of course, you'd have to disable the ability to type or paste into the textbox without setting enabled = false.

...with ReadOnly to True (using the TextBox)
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;

have a look at the url http://i.stack.imgur.com/1M4wH.png
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now