Drag-and-Drop Support
You can enable user drag-and-drop operations within an AlterNET UI application by handling a series of events, most notably the DragEnter, DragLeave, and DragDrop events.
Drag-and-drop events
There are two categories of events in a drag-and-drop operation: events that occur on the current target of the drag-and-drop operation and events that occur on the source of the drag-and-drop operation. To perform drag-and-drop operations, you must handle these events. By working with the information available in the event arguments of these events, you can easily facilitate drag-and-drop operations.
Events on the current drop target
The following table shows the events that occur on the current target of a drag-and-drop operation.
Mouse Event | Description |
---|---|
DragEnter | This event occurs when an object is dragged into the control's bounds. The handler for this event receives an argument of type DragEventArgs. |
DragOver | This event occurs when an object is dragged while the mouse pointer is within the control's bounds. The handler for this event receives an argument of type DragEventArgs. |
DragDrop | This event occurs when a drag-and-drop operation is completed. The handler for this event receives an argument of type DragEventArgs. |
DragLeave | This event occurs when an object is dragged out of the control's bounds. The handler for this event receives an argument of type EventArgs. |
The DragEventArgs class provides the location of the mouse pointer, the current state of the mouse buttons and modifier keys of the keyboard, the data being dragged, and DragDropEffects values that specify the operations allowed by the source of the drag event and the target drop effect for the operation.
Performing drag-and-drop
Drag-and-drop operations always involve two components, the drag source and the drop target. To start a drag-and-drop operation, designate a control as the source and handle the MouseDown event. In the event handler, call the DoDragDrop method providing the data associated with the drop and the a DragDropEffects value.
Set the target control's AllowDrop property set to true
to allow that control to
accept a drag-and-drop operation. The target handles two events. First, an event in response to the drag being over the
control, such as DragOver. And a second event which is the drop action itself,
DragDrop.
The following example demonstrates a drag from a Label control to a
TextBox. When the drag is completed, the TextBox
responds by assigning the label's text to
itself.
textBox1.AllowDrop = true;
// ...
// Initiate the drag
private void label1_MouseDown(object sender, MouseEventArgs e) =>
DoDragDrop(((Label)sender).Text, DragDropEffects.Copy);
// Set the effect filter and allow the drop on this control
private void textBox1_DragOver(object sender, DragEventArgs e) =>
e.Effect = DragDropEffects.Copy;
// React to the drop on this control
private void textBox1_DragDrop(object sender, DragEventArgs e) =>
textBox1.Text = (string)e.Data.GetData(DataFormats.Text);
Dragging Data
All drag-and-drop operations begin with dragging. The functionality to enable data to be collected when dragging begins is implemented in the DoDragDrop method.
In the following example, the MouseDown event is used to start the drag operation because it is the most intuitive (most drag-and-drop actions begin with the mouse button being pressed). However, remember that any event could be used to initiate a drag-and-drop procedure.
To start a drag operation
- 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. For more information, see Data and Effect.
The following example shows how to initiate a drag operation. The control where the drag begins is a Button control, the data being dragged is the string representing the Text property of the Button control, and the allowed effects are either copying or moving.
private void button1_MouseDown(object sender, Alternet.UI.MouseEventArgs e)
{
button1.DoDragDrop(button1.Text, DragDropEffects.Copy |
DragDropEffects.Move);
}
Dropping Data
Once you have begun dragging data from a location on a window or control, you will naturally want to drop it somewhere. The cursor will change when it crosses an area of a window or control that is correctly configured for dropping data. Any area within a window or control can be made to accept dropped data by setting the AllowDrop property and handling the DragEnter and DragDrop events.
To perform a drop
Set the AllowDrop property to true.
In the
DragEnter
event for the control where the drop will occur, ensure that the data being dragged is of an acceptable type (in this case, Text). The code then sets the effect that will happen when the drop occurs to a value in the DragDropEffects enumeration. For more information, see Effect.
private void textBox1_DragEnter(object sender, Alternet.UI.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.
In the example below, a TextBox control is the control being dragged to (where the drop will occur). The code sets the Text property of the TextBox control equal to the data being dragged.
private void textBox1_DragDrop(object sender, Alternet.UI.DragEventArgs e)
{
textBox1.Text = e.Data.GetData(DataFormats.Text).ToString();
}