Code snippet:

private void listBox1_MouseDown(object sender, MouseEventArgs e) { listBox1.DoDragDrop(listBox1.SelectedItem, DragDropEffects.Move); } 

But at the same time, the SelectedItem always equal to 1. The focus does not switch with the mouse only from the keyboard. How to make it possible to select the line with the mouse?

    1 answer 1

    In order to simply change the value of an element, I think that you need to change the value of DragDropEffects from Move to Copy .

    • Copy - The data from the drag source is copied to the target object.
    • Move - Data from the drag source is moved to the target object.

    And it would not hurt to check whether we clicked down and if there is where to go, as well as get the index of the mouse element, something like this:

     private void listBox1_MouseDown(object sender, MouseEventArgs e) { int indexOfItem = listBox1.IndexFromPoint(eX, eY); if(indexOfItem >= 0 && indexOfItem < listBox1.Items.Count) { listBox1.DoDragDrop(listBox1.Items[indexOfItem], DragDropEffects.Copy); } } 

    Link to MSDN source for study: Control.DoDragDrop method