private void listBox1_MouseDown(object sender, MouseEventArgs e) { if (listBox1.SelectedItem != null) { listBox1.DoDragDrop(listBox1.SelectedItem, DragDropEffects.Move); } } private void listBox2_DragOver(object sender, DragEventArgs e) { e.Effect = DragDropEffects.Move; } private void listBox2_DragDrop(object sender, DragEventArgs e) { object item = e.Data.GetData(typeof(string)); listBox2.Items.Add(item); listBox1.Items.Remove(item); }
private void copyToClipboardButton_Click(object sender, EventArgs e) { Clipboard.Clear(); string textToCopy = "Hello World!"; DataObject dataObject = new DataObject(); dataObject.SetText(textToCopy); Clipboard.SetDataObject(dataObject); }In summary, the System.Windows.Forms.DataObject package library is a built-in class that provides a container for data. It is used to facilitate the transfer of data between controls within a Windows Forms application. The above examples show how to use the DataObject class for drag and drop and clipboard operations.