Earlier this week, I ran across a forum post asking about how one would go about creating multiple RadListBoxes that supported multi-item drag and drop. This sounded like a fun challenge, so I decided to take it on. Referencing an earlier forum post that described single-item drag and drop, I came up with the following solution.

Calling DoDragDrop() immediately changes the mouse pointer and readies the RadListBox for the drag-and-drop operation. Originally, I had tried calling this function in the MouseDown event, but this lead to unpredictable clunky behavior due to the fact that DoDragDrop() was getting called before I had the chance to select multiple items. In the following code, I’ve set up the DoDragDrop() method to get called in the MouseMove event when the mouse button is currently down. I also made sure to keep track of in-progress drag operations in order to prevent calling the DoDragDrop() method hundred of times. (Note: Both RadListBoxes must be subscribed to these events)

bool isMouseDown = false
bool isDragAndDrop = false
private void radListBox_MouseDown(object sender, MouseEventArgs e) 
    isMouseDown = true
 
private void radListBox_MouseMove(object sender, MouseEventArgs e) 
    if (!isMouseDown || isDragAndDrop) return
 
    RadListBox listBox = sender as RadListBox; 
    listBox.DoDragDrop(listBox.SelectedItems.ToList(), DragDropEffects.Copy); 
 
    isDragAndDrop = true
 
private void radListBox_MouseUp(object sender, MouseEventArgs e) 
    isMouseDown = false
    isDragAndDrop = false

 

In the DragDrop event, I simply iterated through the list of RadItems I passed to the event through the DoDragDrop() method, swapping each item from its former RadListBox to the new RadListBox.

private void radListBox_DragOver(object sender, DragEventArgs e) 
    e.Effect = DragDropEffects.Copy;  
 
private void radListBox_DragDrop(object sender, DragEventArgs e) 
    RadListBox listBox = sender as RadListBox; 
 
    List<RadItem> items = (List<RadItem>)e.Data.GetData(typeof(List<RadItem>)); 
    RadListBox sourceListBox = items[0].ElementTree.Control as RadListBox; 
 
    foreach (RadItem lstBoxItem in items) 
    { 
        sourceListBox.Items.Remove(lstBoxItem); 
        listBox.Items.Add(lstBoxItem); 
    } 

 

Try it out yourself, or click here to download the source code!

Related Posts

Comments

Comments are disabled in preview mode.