When interaction is required for data transfer between products, or products to be imported from an external file, is achieved by dragging the control is a good choice. On the UI supports drag and drop controls, can greatly improve the user experience.
Dragging itself is not a mystery, it is the nature of the actual process of data exchange. Control accepts the data coming from other places, and processed. There are several methods of data exchange, Windows Clipboard is probably the most used, but the most unnoticed method.
Here implemented using C # drag control, and exchange data via the clipboard.
For drag and drop objects, it is necessary MouseDown or ItemDrag call the DoDragDrop , passing data objects to be dragged and dragged trigger. In general, when the user calls DoDragDrop method proceeds to a loop. The cycle has been tracking the mouse, the window to check whether the mouse is achieved IDropTarget, if realized then: Call DropEnter , and to show the effect by calling GiveFeedBack; when the mouse over the control, call DropOver , but also to show the effect by GiveFeedBack; in tow pull process, a change in the keyboard or mouse button, you can check whether it can continue to operate through QueryContinueDrag, depending on the results returned, calling DropOver or DropLeave ; triggered when the mouse is released DragDrop event, performing the drag logic.
- DragEnter, DragOver, DragLeave Event
Trigger: When the object with the mouse to drag a window control, the DragEnter triggered first, then DragOver, drag and drop objects suspended in a drop area, multiple triggers within the mobile DragOver drop zone, DragLeave triggered when leaving the form.
Role: Set to determine whether the object is and the mouse style accepted. - DragDrop event
trigger: Fires when the user drags an object onto the control and release.
Action: accepting drag data, drag logic implemented
A typical process control to achieve drag
- AllowDrop set
in the control of a drag and drop programming, we must AllowDrop property is set to True - Drag the object triggers DragDrop
private void listBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.listBox1.DoDragDrop("Drag Data", DragDropEffects.Move);
}
- Drag the target response DragDrop
private void listBox2_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
{
//设置DragDrop效果
e.Effect = DragDropEffects.Move;
}
}
private void listBox2_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
//执行DragDrop逻辑
this.listBox2.Items.Add(e.Data.GetData(DataFormats.Text));
this.listBox1.Items.Remove(e.Data.GetData(DataFormats.Text));
}
Control dragged parameters
public DragDropEffects DoDragDrop ( Object data,DragDropEffects allowedEffects)
- data: the user to drag the data content. To be dragged content must be passed to the first argument of this method position. When not required, such as data transfer between different applications, by means of the clipboard .
Controls drag effects
Control dragged effects specified by the DragDropEffects enumeration. - DragDropEffects Description
Member name | Explanation |
---|---|
All | Combination of Copy, Move, and Scroll Effect |
Copy | Copying data of the drag source to the drop target, the icon with the top right corner of a box + |
Link | The data link drag source to the drop target, the icon is a shortcut icon data |
Move | The drag source's mobile data to the drop target, the icon is a block |
None | The drop target does not accept the data, forbidding flag icon |
Scroll | As you drag, if there is rolling entry can scroll target to target a placement in the target not currently visible |
[Demo]
- interface
- Function
drag between the implementation file drag and applications.
Original: Large column C # drag and drop controls to achieve the effect (How to DragDrop Control in C # )