Copy Text from Label – Winforms – DotNet

A customer asked that some of the data on one of their windows forms screens be copy / paste friendly. “Easy enough” you’d think, but on closer inspection I found that the data was being displayed in labels – not text boxes. Nothing wrong with that, but there is no built in way to be able to click on a label and drag your mouse to select the contents of the label so I was tasked with either rewriting the screen to use some sort of disabled textboxes trick or making it possible to click on a label and copy the text of the label to the clipboard.

I chose the copy/paste label route. Here’s what I did. Code follows the steps.

  1. I added a formwide variable that would hold the value to be placed in the clipboard (the value of the label that had been chosen).
  2. Then I added a contextmenustrip to the form and wired up each label to use that contextmenustrip.
  3. When the context menu opens I get the label that launched the context menu and I store the value of that label to that formwide “ToCopy” variable. I also set the text of the context menu option to say both “Copy” and also the value that will be copied to the clipboard.
  4. Finally, I set the ItemClicked method of the ContextMenuStrip to copy the value from the formwide variable (which we set when the context menu opened) to the clipboard.
  5. Consume Beverage of Choice


Dim valueToCopyToClipBoard As String = ""

Private Sub ContextMenuForCopyLabelData_Opening(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ContextMenuForCopyLabelData.Opening
ContextMenuForCopyLabelData.Items.Clear()
Dim lbl As Label = ContextMenuForCopyLabelData.SourceControl
valueToCopyToClipBoard = lbl.Text
ContextMenuForCopyLabelData.Items.Add(String.Format("Copy - {0}", lbl.Text))
End Sub

Private Sub ContextMenuForCopyLabelData_ItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles ContextMenuForCopyLabelData.ItemClicked
Clipboard.SetDataObject(valueToCopyToClipBoard, True)
End Sub

Modifying the data to be displayed in textboxes would not have been a huge task, but it would be boring and required doing the same exact action a lot of times. Not hard, just a lot of work for a little gain. Plus, textboxes don’t autogrow so longer data would have to be handled some way making it even more tedious. The nice thing about textboxes is that it would have “just worked” without have to handle any special events. My plan if I went the text box route was basically to create a custom textbox control that matched the form, but was readonly, had no border, etc.

Default values would be something like this:

.ReadOnly = true;
.BorderStyle = 0;
.BackColor = this.BackColor;
.TabStop = false;

Then for each textbox that you wanted to hold readonly data and look like a label you’d just use that bad boy.

Each solution is good for a particular situation. If you have data of different lengths going into to your controls then labels are probably the way to go. But in the end it’s up to you.

Hope you find it useful!

The research I did consisted covered essentially these 2 pages:
https://msdn.microsoft.com/en-us/library/system.windows.forms.contextmenu.sourcecontrol%28v=vs.110%29.aspx
(Just a little documentation on how to find out which control actually launched the ContextMenu)

http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_21693232.html
It turns out my solution looked a lot like theirs in the end, but in my solution we never talk about a specific control so it can be attached to any label (and any number of labels) and just work being written one time.

Have you encountered the same problem and found this useful? Or maybe attacked it a better way. Let me know in the comments below!

1 Comment

  1. I am developing a application using C#. I have a window which has a label containing some text. I want to copy as we copy something from anywhere. But i cant copy of the label from the window. How can i do that to copy the text of the label???

Submit a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>