Listview with copy

Is there an easy way to add a copy-paste for a list, or should I just switch to a DataGridView?

My application looks like an address book, contains emails, numbers, etc., where it will be useful to copy a copy.

+7
c # copy-paste listview winforms
source share
3 answers

The example below treats Ctrl-C as a copy in the clipboard command and copies the second column value from all the selected rows:

private void resultsListView_KeyUp(object sender, KeyEventArgs e) { if (sender != resultsListView) return; if (e.Control && e.KeyCode == Keys.C) CopySelectedValuesToClipboard(); } private void CopySelectedValuesToClipboard() { var builder = new StringBuilder(); foreach (ListViewItem item in resultsListView.SelectedItems) builder.AppendLine(item.SubItems[1].Text); Clipboard.SetText(builder.ToString()); } 

Literature:

+11
source share

It’s not very difficult to do manual copying and pasting, just put an event handler for KeyDown (or maybe KeyPress cannot remember it, but it’s sure enough that this is one of them) and check which key is pressed while looking at e.KeyCode and check is e.Control true. If one of x , c or v just calls Clipboard.SetText or Clipboard.GetText to write / read to / from the clipboard.
See here for MSDN documentation for the Clipboard class.

You can also add a context menu using Copy and Paste to the List to complete it.

+1
source share

My requirement was to add a button to copy the list of serial numbers to the clipboard. I added a button in normal mode, and then added the following method:

 private void btnClipboard_Click(object sender, EventArgs e) { String clipText = string.Empty; foreach (ListViewItem item in lstSerials.Items) { clipText += item.SubItems[0].Text; clipText += Environment.NewLine; } if (!String.IsNullOrEmpty(clipText)) { Clipboard.SetText(clipText); } } 
0
source share

All Articles