In the MouseDown event for the control where the drag will begin, use the DoDragDrop method to set the data to be dragged and the allowed effect dragging will have.
private void listBoxA_MouseDown(object sender, MouseEventArgs e)
{
listBoxA.DoDragDrop(listBoxA.SelectedItem, DragDropEffects.Copy)
}
Step 2: Perform Drop Operation
- Set the AllowDrop property of ListBoxB to true.
- In the DragEnter event for the ListBoxB where the drop will occur, ensure that the data being dragged is of an acceptable type (in this case of Listbox- Text). The code then sets the effect that will happen when the drop occurs to a value in the DragDroeffects enumeration that has these option:
All
Copy
Link
Move
Scroll
private void listBoxB_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
- In the DragDrop event for the control where the drop will occur, use the GetData method to retrieve the data being dragged.
private void listBoxB_DragDrop(object sender, DragEventArgs e)
{
listBox2.Items.Add(e.Data.GetData(DataFormats.Text).ToString());
}