views:

121

answers:

1

Hi,

Is there an easy way of adding copy-paste for a listview, or should I just switch to DataGridView instead?

My application is kinda like an address book, it contains emails, numbers etc where copy paste would be useful.

A: 

It's not very difficult to do manual copy and paste, just put in an event handler for KeyDown (or maybe it's KeyPress can't remember but fairly sure it's one of them) and check what key is pressed by looking at e.KeyCode and check if e.Control is true. If it's one of x, c or v just call Clipboard.SetText or Clipboard.GetText to write/read to/from the clipboard.
See here for the MSDN documentation of the Clipboard class.

You could add a context menu with Copy and Paste on to the ListView also to make it complete.

ho1
The problem with that is I only manage to copy the entire row, unless there is some neat tricks for selecting a cell.
Zubirg
@Zubirg: Yes, I forgot that you don't really have cells in a `ListView`, it might be easier to switch to a `DataGridView`. However, if you do want to stick with the `ListView` you could do something like handling `Click` and `MouseDown` events on it to calculate what subitem was clicked and then changing the `BackColor` of that subitem to make it look selected and then you could handle copy and paste for just that subitem.You'd have to make sure to set `UseItemStyleForSubItems = false;` on the Item though, otherwise the colours won't show on the subitems.
ho1