private void CopyText() { string data = string.Empty; foreach (ListViewItem it in listView1.Items) { data += it.Text + "\t" + it.SubItems[1].Text + "\t" + it.SubItems[2].Text + Environment.NewLine; } ClipboardCopy.CopyDataToClipboard(data); }
/// <summary> /// Copies data from items and sub items in the list view to the clipboard. /// </summary> /// <param name="data">Data to copy.</param> static public void CopyDataToClipboard(object data) { string str = string.Empty; if (data is string) { str = data as string; } else if (data != null && data.GetType().IsArray) { foreach (object it in (Array)data) { str += it.ToString(); } } else { ListView view = data as ListView; if (view != null) { if (view.VirtualMode) { StringBuilder sb = new StringBuilder(view.SelectedIndices.Count * 50); foreach (int pos in view.SelectedIndices) { ListViewItem it = view.Items[pos]; foreach (ListViewItem.ListViewSubItem sub in it.SubItems) { sb.Append(sub.Text); sb.Append('\t'); } sb.Remove(sb.Length - 1, 1); sb.Append(Environment.NewLine); } str = sb.ToString(); } else { StringBuilder sb = new StringBuilder(view.SelectedIndices.Count * 50); foreach (ListViewItem it in view.SelectedItems) { foreach (ListViewItem.ListViewSubItem sub in it.SubItems) { sb.Append(sub.Text); sb.Append('\t'); } sb.Remove(sb.Length - 1, 1); sb.Append(Environment.NewLine); } str = sb.ToString(); } } } if (!string.IsNullOrEmpty(str)) { ClipboardCopy threadHelper = new ClipboardCopy(str); ThreadStart ts = new ThreadStart(threadHelper.CopyToClipboard); Thread STAThread1 = new Thread(ts); STAThread1.SetApartmentState(ApartmentState.STA); STAThread1.Start(); STAThread1.Join(); } }