3

I have a Windows Form program for contact list. I already have a context menu used for copy and pasting from the DataGridView.
However, I want to be able to right click a Label and select copy from a context menu to copy the data from that ONE Label.
I have 10 different Labels, I do NOT want all of them, just the one that I right clicked on to select copy.

I know that using Clipboard.SetText(label1.text) will let me select that specific Label, but I do not what to create 10 context Labels that I should be able to do with one.

If I wanted to select all of the text boxes I can do this.

string UserInfo = $"{lblFirstName.Text}\n" +
                  $"{lblLastName.Text}\n" +
                  $"{lblEmailAddress.Text}\n" +
                  $"{lblPhysicalAddress.Text}\n" +
                  $"{lblCountry.Text}\n" +
                  $"{lblCompany.Text}\n" +
                  $"{lblStatus.Text}\n" +
                  $"{lblFirstContact.Text}\n" +
                  $"{lblLastContact.Text}\n" +
                  $"{lblNotes.Text}\n ";
Clipboard.SetText(UserInfo);

For the DataGridView was easy. But this is for the use to right click on ONE Label to do the copy.

I created a 2nd ContextMenuStrip and what SHOULD occur:

  1. right click on labelA
  2. Context menu pops up with copy, and select it
  3. System recognizes that labelA was right clicked on so takes the text from the Label. Clipboard.SetText(labelChosen)
  4. then if user wants to click labelC that will be choosen.

I just do not want to create 10 context menus to do this.

Jimi
  • 29,621
  • 8
  • 43
  • 61
John B
  • 120
  • 1
  • 9
  • Can you share the code part which you'd tried? – dhilmathy Feb 10 '19 at 22:33
  • Get a `ContextMenuStrip` from the ToolBox, drop it on the Form, select the Label you want to attach the Menu to, set the Label's `ContextMenuStrip` property to the new Menu. – Jimi Feb 10 '19 at 22:37
  • I have only found how to select ALL of the labels. but I want to select only ONE label. I do know that I can drag and drop 10 different context menus for each label. but that would be wasteful. – John B Feb 10 '19 at 22:50
  • The [ContextMenuStrip.SourceControl](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.contextmenustrip.sourcecontrol) references the control that invoke the menu. For the menu items, see here: [Get Source Control Of DropDownMenu](https://stackoverflow.com/a/53263702/7444103). – Jimi Feb 10 '19 at 23:36
  • @Jimi I think the OP is using `ContextMenu` class, not `ContextMenuStrip`, which, sadly, doesn't appear to have the `SourceControl` property. – CoolBots Feb 11 '19 at 00:21
  • @CoolBots Yes, it does. – Jimi Feb 11 '19 at 00:28
  • Jimi, you answer is the most simple. and worked easily. this is the 2nd time you have answered in comments and made me do reading!! thanks this is helping. can you put your item in the answer area so I can accept it. this is the final item using "contextMenuStripLabels.SourceControl.Text" private void copyLabelOnlyToolStripMenuItem_Click(object sender, EventArgs e) { Clipboard.SetText(contextMenuStripLabels.SourceControl.Text); } – John B Feb 11 '19 at 00:33
  • @Jimi not according to [ContexMenu docs](https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.contextmenu?view=netframework-4.7.2#properties), [MenuItem docs](https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.menuitem?view=netframework-4.7.2), or VS2017 intelliSense... – CoolBots Feb 11 '19 at 00:34
  • 1
    @CoolBots That control is for WPF, not WinForms. But, as a suggestion, since you're already *working* with John B to solve this, you could show an implementation of the `ContextMenuStrip`, since the OP apparently doesn't dislike it. – Jimi Feb 11 '19 at 00:36
  • The simplest answer shown is the one that I will be glad to acknowledged answer. Most everyone on here is pretty helpful. and this contextMenuStripLabels.SourceControl.Text answer has not showed up anywhere I can find. – John B Feb 11 '19 at 00:43

1 Answers1

2

EDITED - Thanks to @Jimi for this suggestion, via comments

Simplest solution is to add the ContextMenuStrip control to your Form from the toolbox, and configure an item - "Copy"; double-click the item, and use the following code in the event handler (presuming your context menu strip is called labelContextMenuStrip):

Clipboard.SetText(labelContextMenuStrip.SourceControl.Text);

You can then assign the ContextMenuStrip to each desired label's ContextMenuStrip property in the designer, or programmatically, in your Form's Load or Shown event:

foreach (var label in Controls.OfType<Label>())
{
    label.ContextMenuStrip = labelContextMenuStrip;
}

Full code (verified solution):

private void Form1_Load(object sender, EventArgs e)
{
    // Optional - can be manually set in the Designer
    foreach (var label in Controls.OfType<Label>())
    {
        label.ContextMenuStrip = labelContextMenuStrip;
    }
}

private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
    Clipboard.SetText(labelContextMenuStrip.SourceControl.Text);
}
CoolBots
  • 4,770
  • 2
  • 16
  • 30
  • How would the Click Event be used for the Right click of the mouse? this does not work. I have contextmenu selected to the label, so right click brings that up, and I can select copy. – John B Feb 10 '19 at 22:55
  • @JohnB context menu items, like many other standard .Net UI items, have a `Tag` field that is free to use. You can put it in there at the moment you build the context menu in the right-click code. – Nyerguds Feb 10 '19 at 23:10
  • @JohnB I modified my answer to include the context menu solution (verified). I understand the difficulty you ran into - how to keep track of which label caused the click. Sorry, I misunderstood the question initially; let me know if this answer works for you now. – CoolBots Feb 11 '19 at 00:07
  • I clarified my edit to make sure it was understood I was using context menu strip. I am still quite a bit a newb with your answer. Jimi's using the contextmenustrip.source was a lot simpler. – John B Feb 11 '19 at 00:37
  • Not a problem - I agree Jimi's solution is better, and `ContextMenuStrip` is definitely the way to go – CoolBots Feb 11 '19 at 00:38
  • @CoolBots, I am still a newb at programming, but trying to get a better understanding. Especially with asking the right question. I searched for an hour through the debug trying to find the section where I could type a "." and then option. Microsoft is making C# very easy in programming, but with so many ways to do this it gets a overwhelming. I really do appreciate your help. it made me research more. – John B Feb 11 '19 at 00:48
  • JohnB, per @Jimi's suggestion, I revised my answer to match your question. I did leave the programmatic way of attaching the ContextMenuStrip to each label (and commented it as optional), in case that interests you. I am glad you found my contribution useful - that's really the goal here @ StackOverflow! – CoolBots Feb 11 '19 at 01:10
  • 1
    Thank you CoolBots and @Jimi for your work on this. Once I get to 15 rep I can start up voting answers provided. I thought it was 10. So hopefully people will see this question, being unique and not a duplicate, and I can get some upvotes. I will use the links provided to help others in other areas. – John B Feb 11 '19 at 01:29