C # implement the DragDrop function of the control

Source: Internet
Author: User

1. Drag the control content to other controls
During the development process, the customer often asks to drag the data of a control to another control. For example, drag data from one ListBox to another. You can also drag the data in the DataGridView to a node in the TreeView.
In an application, you can perform drag-and-drop operations in a Windows application by handling a series of events, such as DragEnter, DragLeave, and DragDrop events. By using the available information in these event parameters, you can easily perform drag-and-drop operations.

The drag-and-drop operation is implemented in three steps in the Code. The first step is to start the drag-and-drop operation, implement the MouseDown event response code on the control that needs to be dragged, and call the DoDragDrop () method; the second is to implement the drag-and-drop effect. Add the DragEnter event response code to the target control, and use the DragDropEffects Enumeration type to implement the drag effect, such as moving or copying. Finally, the data placement operation is performed, add the DragDrop response code to the target control and add the data to the control.
 
1 using System;
2 using System. Drawing;
3 using System. Collections;
4 using System. ComponentModel;
5 using System. Windows. Forms;
6 using System. Data;
7
8 namespace DragDrop
9 {
10 /// <summary>
11 /// Summary of Form1.
12 /// </summary>
13 public class Form1: System. Windows. Forms. Form
14 {
15 private System. Windows. Forms. ListBox listBox1;
16 private System. Windows. Forms. ListBox listBox2;
17 /// <summary>
18 /// required designer variables.
19 /// </summary>
20 private System. ComponentModel. Container components = null;
21
22 public Form1 ()
23 {
24 //
25 // required for Windows Form Designer support
26 //
27 InitializeComponent ();
28
29 //
30 // TODO: add Any constructor code after InitializeComponent calls
31 //
32}
33
34 /// <summary>
35 // clear all resources in use.
36 /// </summary>
37 protected override void Dispose (bool disposing)
38 {
39 if (disposing)
40 {
41 if (components! = Null)
42 {
43 components. Dispose ();
44}
45}
46 base. Dispose (disposing );
47}
48
49 # region code generated by Windows Form Designer
50 /// <summary>
51 // The designer supports the required methods-do not use the code editor to modify
52 // content of this method.
53 /// </summary>
54 private void InitializeComponent ()
55 {
56 this. listBox1 = new System. Windows. Forms. ListBox ();
57 this. listBox2 = new System. Windows. Forms. ListBox ();
58 this. SuspendLayout ();
59 //
60 // listBox1
61 //
62 this. listBox1.ItemHeight = 12;
63 this. listBox1.Location = new System. Drawing. Point (32, 24 );
64 this. listBox1.Name = "listBox1 ";
65 this. listBox1.Size = new System. Drawing. Size (120,280 );
66 this. listBox1.TabIndex = 0;
67 this. listBox1.MouseDown + = new System. Windows. Forms. MouseEventHandler (this. listbox#mousedown );
68 //
69 // listBox2
70 //
71 this. listBox2.ItemHeight = 12;
72 this. listBox2.Location = new System. Drawing. Point (248, 24 );
73 this. listBox2.Name = "listBox2 ";
74 this. listBox2.Size = new System. Drawing. Size (120,280 );
75 this. listBox2.TabIndex = 0;
76 this. listBox2.DragDrop + = new System. Windows. Forms. DragEventHandler (this. listBox2_DragDrop );
77 this. listBox2.DragEnter + = new System. Windows. Forms. DragEventHandler (this. listBox2_DragEnter );
78 //
79 // Form1
80 //
81 this. AutoScaleBaseSize = new System. Drawing. Size (6, 14 );
82 this. ClientSize = new System. Drawing. Size (408,333 );
83 this. Controls. Add (this. listBox1 );
84 this. Controls. Add (this. listBox2 );
85 this. Name = "Form1 ";
86 this. Text = "Form1 ";
87 this. Load + = new System. EventHandler (this. Form1_Load );
88 this. ResumeLayout (false );
89
90}
91 # endregion
92
93 private void Form1_Load (object sender, System. EventArgs e)
94 {
95 this. listBox1.AllowDrop = true;
96 this. listBox2.AllowDrop = true;
97 this. listBox1.Items. Add ("");
98 this. listBox1.Items. Add ("B ");
99 this. listBox1.Items. Add ("c ");
100}
101
102 private void listbox#mousedown (object sender, System. Windows. Forms. MouseEventArgs e)
103 {
104 this. listBox1.DoDragDrop (this. listBox1.Items [this. listBox1.SelectedIndex], DragDropEffects. Move );
105}
106
107 private void listBox2_DragEnter (object sender, System. Windows. Forms. DragEventArgs e)
108 {
109 if (e. Data. GetDataPresent (DataFormats. Text ))
110 {
111 e. Effect = DragDropEffects. Move;
112}
113}
114
115 private void listBox2_DragDrop (object sender, System. Windows. Forms. DragEventArgs e)
116 {
117 this. listBox2.Items. Add (e. Data. GetData (DataFormats. Text ));
118 this. listBox1.Items. Remove (e. Data. GetData (DataFormats. Text ));
119}
120}
121}
 
 
 
2. Drag the file to the control to obtain the file path.
Drag and Drop a file or directory to your program. The user experience is good.
Get the code for the dragged path: (System. Array) e. Data. GetData (DataFormats. FileDrop ).
Then you can copy and paste these paths.
  
 
1 using System;
2 using System. Collections. Generic;
3 using System. ComponentModel;
4 using System. Data;
5 using System. Drawing;
6 using System. Linq;
7 using System. Text;
8 using System. Windows. Forms;
9
10 namespace TestFileDrag
11 {
12 public partial class Form1: Form
13 {
14 public Form1 ()
15 {
16 InitializeComponent ();
17}
18
19 private void Form1_Load (object sender, EventArgs e)
20 {
21 SetCtrlDrag. SetCtrlDragEvent (this. textBox1 );
22}
23}
24
25 public class SetCtrlDrag
26 {
27 public static void SetCtrlDragEvent (Control ctrl)
28 {
29 if (ctrl is TextBox)
30 {
31 TextBox tb = ctrl as TextBox;
32 tb. AllowDrop = true;
33 tb. DragEnter + = (sender, e) =>
34 {
35 e. Effect = DragDropEffects. Link; // The icon when dragging.
36 };
37 tb. DragDrop + = (sender, e) =>
38 {
39 (TextBox) sender). Text = (System. Array) e. Data. GetData (DataFormats. FileDrop). GetValue (0). ToString ();
40 };
41}
42}
43}
44}
 
 
Interface Effect
 
 
 
Iii. Description
Msdn: http://msdn.microsoft.com/zh-cn/library/system.windows.forms.dragdropeffects.aspx
1. Method
When implementing the drag-and-drop effect, C # provides the DoDragDrop method, which is used to start the drag-and-drop operation. This method is defined by the Control class, since controls are directly or indirectly derived from the Control class, developers can call the DoDragDrop method in any visualization component. The DoDragDrop method uses the following syntax:
Public DragDropEffects DoDragDrop (Object data, DragDropEffects allowedEffects)
Data: the data content to be dragged. The content to be dragged must be passed into the first parameter location of this method.
AllowedEffects: One of the DragDropEffects enumerated values. This type contains the effects of the drag operation. The DragDropEffects enumerated values are shown in Table 32.8.
DragDropEffects enumerated values:
Enumeration value description
All copies, removes, and rolls data from the drag source to the target.
Copy Copies data to the destination
Link: Link the data in the drag source to the destination
Move: Move the data from the drag source to the placement target.
None the destination does not accept the data.
Scroll is about to start rolling in the placement target, or is currently rolling

When using the DoDragDrop method, developers must specify the allowedEffects parameter as any member of the table. In addition, they can use bitwise operators to pass in any member of the table as a complete parameter, to get the desired drag effect, the key code is as follows:
DragDropEffects. Copy | DragDropEffects. None
 
2. Events
C # provides a system drag-and-drop event, which can be used with the drag-and-drop method to achieve better results. Common drag-and-drop events are shown in the table.
Table drag-and-drop events
Description
DragEnter this event is triggered when you drag and drop the mouse cursor to the control for the first time.
DragDrop occurs when the drag and drop operation is completed.
GiveFeedback occurs during the drag operation
DragLeave event is triggered if the user moves out of a window.
DragOver: If the mouse moves but stays in the same control, the DragOver event is triggered.
When the status of the keyboard or mouse button changes during the drag-and-drop operation of QueryContinueDrag, The QueryContinueDrag event is triggered. QueryContinueDrag event allows you to drag the source to determine whether to cancel the drag-and-drop operation

 


From SamWang

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.