|
C# • Initiate Drag-and-Drop Listing 2. Initiating a drag-and-drop operation from a source control isn't difficult. In this particular case, you perform a move operation, which removes the dragged value from the source control. Note that this approach won't work unless the destination control implements the event handlers for the DragEnter and DragDrop events. private void Players_MouseDown(object sender, MouseEventArgs e)
{
if ( lstPlayers.SelectedIndex >=0)
{
string players;
int ndx = lstPlayers.SelectedIndex;
DragDropEffects effect;
players = lstPlayers.Items[ndx].ToString();
if(players != "")
{
// Permit target to move or copy data
effect = lstPlayers.DoDragDrop(players,
DragDropEffects.Move | DragDropEffects.Copy);
// Remove item from ListBox since move occurred
if (effect == DragDropEffects.Move)
lstPlayers.Items.RemoveAt(ndx);
}
}
}
|