Click here to Skip to main content
15,906,463 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use this code:

C#
ListViewItem clone;
int hasclone = 0;


Copy:
C#
foreach (ListViewItem item in listView1.SelectedItems)
               {
                   clone = (ListViewItem)item.Clone();
                   hasclone = 1;
               }


Paste:
C#
if (hasclone == 1)
{
    hasclone = 0;
        nummer = nummer + 1;
        listView1.Items.Insert(actions, clone);
        actions = actions + 1;
        sortlist();
}


I can copy and paste a row in the listview like this,

BUT

It only pastes one (or copies one)

Can someone help me with this?

For example:
Listview
Item1 Morestuff Morestuff
Item2 Morestuff Morestuff

When i copy Item1 and Item2

It pastes Item2 as Item3

So basicly it only copys the last one because of

C#
foreach (ListViewItem item in listView1.SelectedItems)
               {
                   clone = (ListViewItem)item.Clone();
                   hasclone = 1;
               }


Wich i understand so it can only copy 1 and so only paste 1 listview item

I would like to be able to copy and paste multiple items
Posted

1 solution

Try something like:
Change how you store the copy:
C#
List<ListViewItem> clones = new List<ListViewItem>();

Variable hasclone is unnecessary.
Then copy them all:
C#
clones.Clear();
foreach (ListViewItem item in listView1.SelectedItems)
{
  clones.Add((ListViewItem)item.Clone());

}

And paste them all:
C#
if (clones.Any())   //.Any() is from System.Linq
{
  ++nummer;  // is this incremented per paste, or per pasted value?
             // if per value, move it inside the foreach below
  foreach (ListViewItem clone in clones)
  {
    listView1.Items.Insert(actions++, clone);
  }
  sortlist();
}
Good luck!
 
Share this answer
 
Comments
Mike Vanderklij 28-Nov-15 7:42am    
Thankyou, wil try this :)
Matt T Heffron 30-Nov-15 13:18pm    
How did it work?
By the way, I just noticed that I forgot to mention that you should clear the clones list (clones.Clear();) after the sortlist() unless you want to allow copy-one-paste-many behavior.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900