如何复制到剪贴板,ListView的多个选定项目?

时间:2021-02-08 19:40:13

This code is working for one single selected item: In the top:

此代码适用于单个选定项目:在顶部:

ContextMenuStrip menuStrip;

Then in the constructor:

然后在构造函数中:

menuStrip = new ContextMenuStrip();
menuStrip.ItemClicked += menuStrip_ItemClicked;
menuStrip.Items.Add("Cut");
menuStrip.Items.Add("Copy");
menuStrip.Items.Add("Paste");

The menuStrip itemclicked event:

menuStrip itemclicked事件:

ListViewItem item;
        private void menuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Text == "Copy")
            {
                Clipboard.SetText(item.SubItems[1].Text);

            }
        }

Then the ListView mouse click event:

然后是ListView鼠标单击事件:

private void lstDisplayHardware_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                item = lstDisplayHardware.GetItemAt(e.X, e.Y);
                menuStrip.Show(lstDisplayHardware, e.Location);
            }
        }

This code is working for single selected item. For example I click on an item in the ListView, right click on the item and select Copy: the selected item's subitem is copied to the Clipboard.

此代码适用于单个选定项目。例如,我单击ListView中的项目,右键单击该项目并选择复制:所选项目的子项目将复制到剪贴板。

But now I want to do the same thing for multiple selection.

但现在我想为多项选择做同样的事情。

So if I use Ctrl+Left mouse click and for example selected 4 items and invoke Copy command from the context menu, I expect all subitems of the 4 selected items text to be copied into the Clipboard.

因此,如果我使用Ctrl +鼠标左键单击并选择4个项目并从上下文菜单中调用复制命令,我希望将4个所选项目文本的所有子项目复制到剪贴板中。

For example I have these items:

例如,我有这些项目:

danny hello world
daniel hi all
dan rain today
daniels sunny day

丹尼你好世界丹尼尔喜所有dan rain今天daniels阳光灿烂的日子

I select the items:

我选择了这些项目:

danny
daniel
dan
daniels

丹尼丹尼丹丹尼尔斯

Then right click and click on Copy. When I paste anywhere from the clipboard I want it to show:

然后右键单击并单击“复制”。当我从剪贴板粘贴任何地方时,我希望它显示:

hello world
hi all
rain today
sunny day

你好世界你好今天阳光灿烂的日子

All of the sub items of the selected items in the same order and format.

所选项目的所有子项目的顺序和格式相同。

3 个解决方案

#1


First, you have to enable multiselect:

首先,您必须启用多选:

ListView1.MultiSelect = true;

Then, you can get selected items with:

然后,您可以获得以下选定项目:

private void menuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
    ListView.SelectedListViewItemCollection selectedItems = 
    ListView1.SelectedItems;
    if (e.ClickedItem.Text == "Copy")
    {
         String text = "";
         foreach ( ListViewItem item in selectedItems )
         {
              text += item.SubItems[1].Text;
         }
         Clipboard.SetText(text);
    }
}

#2


    private void listBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Control && e.KeyCode == Keys.C)
        {
            CopyListBox(listBox1);
        }
    }

    public void CopyListBox(ListBox list)
    {

        StringBuilder sb = new StringBuilder();
        foreach (string item in list.SelectedItems)
        {
            sb.AppendLine(item);
        }

        Clipboard.SetDataObject(sb.ToString());

    }

#3


For ListView, not ListBox code will be:

对于ListView,不是ListBox代码将是:

    private void listView1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Control && e.KeyCode == Keys.C)
        {
            CopyListBox(listView1);
        }
    }
    public void CopyListBox(ListView list)
    {
        StringBuilder sb = new StringBuilder();
        foreach (var item in list.SelectedItems)
        {
            ListViewItem l = item as ListViewItem;
            if (l != null)
                foreach (ListViewItem.ListViewSubItem sub in l.SubItems)
                    sb.Append(sub.Text+"\t");
            sb.AppendLine();
        }
        Clipboard.SetDataObject(sb.ToString());

    }

#1


First, you have to enable multiselect:

首先,您必须启用多选:

ListView1.MultiSelect = true;

Then, you can get selected items with:

然后,您可以获得以下选定项目:

private void menuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
    ListView.SelectedListViewItemCollection selectedItems = 
    ListView1.SelectedItems;
    if (e.ClickedItem.Text == "Copy")
    {
         String text = "";
         foreach ( ListViewItem item in selectedItems )
         {
              text += item.SubItems[1].Text;
         }
         Clipboard.SetText(text);
    }
}

#2


    private void listBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Control && e.KeyCode == Keys.C)
        {
            CopyListBox(listBox1);
        }
    }

    public void CopyListBox(ListBox list)
    {

        StringBuilder sb = new StringBuilder();
        foreach (string item in list.SelectedItems)
        {
            sb.AppendLine(item);
        }

        Clipboard.SetDataObject(sb.ToString());

    }

#3


For ListView, not ListBox code will be:

对于ListView,不是ListBox代码将是:

    private void listView1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Control && e.KeyCode == Keys.C)
        {
            CopyListBox(listView1);
        }
    }
    public void CopyListBox(ListView list)
    {
        StringBuilder sb = new StringBuilder();
        foreach (var item in list.SelectedItems)
        {
            ListViewItem l = item as ListViewItem;
            if (l != null)
                foreach (ListViewItem.ListViewSubItem sub in l.SubItems)
                    sb.Append(sub.Text+"\t");
            sb.AppendLine();
        }
        Clipboard.SetDataObject(sb.ToString());

    }