Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Copy Paste from ListView?

Copy Paste from ListView?

Scheduled Pinned Locked Moved C#
helptutorialquestion
2 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Delegate
    wrote on last edited by
    #1

    Is the a way to do Copy Paste fom a ListView? I couldn't found anything on this, I tried Clipboard but it requires to trap Ctrl + C / Ctrl + P so I don't have any clue how to accomplish this. Anybody know please help me. Thanks in advance.

    H 1 Reply Last reply
    0
    • D Delegate

      Is the a way to do Copy Paste fom a ListView? I couldn't found anything on this, I tried Clipboard but it requires to trap Ctrl + C / Ctrl + P so I don't have any clue how to accomplish this. Anybody know please help me. Thanks in advance.

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Handling key strokes is the easy part. Handle the ListView.KeyUp event and check the KeyEventArgs.KeyCode and KeyEventArgs.Control properties:

      listView1.KeyUp += new KeyEventHandler(OnKeyUp);
      // ...
      private void OnKeyUp(object sender, KeyEventArgs e)
      {
      if (e.Control)
      {
      if (e.KeyCode == Keys.C) Copy();
      else if (e.KeyCode == Keys.P) Paste();
      }
      }

      To copy, grab the selected ListViewItems, pack them in a DataObject, and send to the clipboard. Note that this will only work within the same AppDomain:

      public void Copy()
      {
      DataObject do = new DataObject("ListViewItems", listView1.SelectedItems);
      Clipboard.SetDataObject(do, false);
      }

      To paste, you unpack the copied data (if relevent) and insert into the ListView:

      public void Paste()
      {
      IDataObject do = Clipboard.GetDataObject();
      if (do != null)
      {
      IEnumerable items = (IEnumerable)do.GetData("ListViewItems");
      if (items != null)
      foreach (ListViewItem item in items)
      listView1.Items.Add(item);
      }
      }

      Note that this code is untested but should give you a start of what you need to do. Implementing drag and drop is not much different. Again, remember that this will only work within an AppDomain. To copy and paste ListViewItems between two separate AppDomains (ex: two different instances of a Windows Forms application), you need to marshal the data. You could marshal it as a string (encode the selected ListViewItems as a string, then decode it in the other AppDomain), or a number of other things that would be difficult to do in a managed application (like alloc'ing the items in global memory, though with objects tied to a HANDLE (native pointer) that isn't so easy). This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups