如何仅使用一个上下文菜单从不同标签复制文本 [英] How to copy text from different Labels using only one context menu
问题描述
我有一个用于联系人列表的Windows窗体程序.我已经有一个上下文菜单,用于从DataGridView复制和粘贴.
但是,我希望能够右键单击一个标签,然后从上下文菜单中选择复制"以从该一个标签"中复制数据.
我有10个不同的标签,我不想要所有标签,只是我右键单击以选择副本的标签.
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.
我知道使用Clipboard.SetText(label1.text)
可以让我选择该特定的Label,但是我不打算创建10个上下文标签,而我应该能够对其进行处理.
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);
对于DataGridView来说很简单.但这是用于右键单击一个标签"以进行复制.
For the DataGridView was easy. But this is for the use to right click on ONE Label to do the copy.
我创建了第二个ContextMenuStrip,应该发生什么:
I created a 2nd ContextMenuStrip and what SHOULD occur:
- 右键单击
labelA
- 弹出带有复制的上下文菜单,然后选择它
- 系统识别出右键单击了
labelA
,因此从标签"中获取了文本.Clipboard.SetText(labelChosen)
- 然后,如果用户要单击将选择的
labelC
.
- right click on
labelA
- Context menu pops up with copy, and select it
- System recognizes that
labelA
was right clicked on so takes the text from the Label.Clipboard.SetText(labelChosen)
- then if user wants to click
labelC
that will be choosen.
我只是不想创建10个上下文菜单来做到这一点.
I just do not want to create 10 context menus to do this.
推荐答案
编辑-感谢 @Jimi 的建议,通过评论
EDITED - Thanks to @Jimi for this suggestion, via comments
最简单的解决方案是从工具箱中将ContextMenuStrip
控件添加到窗体中,并配置一个项目-复制";双击该项目,然后在事件处理程序中使用以下代码(假定您的上下文菜单带称为labelContextMenuStrip):
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);
然后,您可以在设计器中,或者以编程方式,在窗体的Load
或Shown
事件中,将ContextMenuStrip分配给每个所需标签的ContextMenuStrip
属性:
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);
}
这篇关于如何仅使用一个上下文菜单从不同标签复制文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!