C # DragDropEffects class and drag implementation

Source: Internet
Author: User
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. Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Linq;
Using System. Text;
Using System. Windows. Forms;

Namespace WinToXml
{
Public partial class FrmMain: Form
{
Public FrmMain ()
{
InitializeComponent ();
}
// Use the Winform text box to drag and drop a directory or file path (C #)

Protected void SetAllTextBox (Control org)
{
Foreach (Control txt in org. Controls)
{
If (txt is TextBox)
{
Txt. AllowDrop = true;
Txt. DragDrop + = new DragEventHandler (txt_ObjDragDrop );
Txt. DragEnter + = new DragEventHandler (txt_ObjDragEnter );
}
Else
{
If (txt. Controls. Count> 0)
{
SetAllTextBox (txt );
}
}
}
}

Private void txt_ObjDragEnter (object sender, DragEventArgs e)
{
E. Effect = DragDropEffects. Link; // The icon when dragging.
}

Private void txt_ObjDragDrop (object sender, DragEventArgs e)
{
(TextBox) sender). Text
= (System. Array) e. Data. GetData (DataFormats. FileDrop). GetValue (0). ToString ();
}

Private void FrmMain_Load (object sender, EventArgs e)
{
SetAllTextBox (this );
}
}
}

Url: http://greatverve.cnblogs.com/archive/2012/03/03/DragDropEffects.html

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.

Using System;
Using System. Drawing;
Using System. Collections;
Using System. ComponentModel;
Using System. Windows. Forms;
Using System. Data;

Namespace DragDrop
{
/// <Summary>
/// Summary of Form1.
/// </Summary>
Public class Form1: System. Windows. Forms. Form
{
Private System. Windows. Forms. ListBox listBox1;
Private System. Windows. Forms. ListBox listBox2;
/// <Summary>
/// Required designer variables.
/// </Summary>
Private System. ComponentModel. Container components = null;

Public Form1 ()
{
//
// Required for Windows Form Designer support
//
InitializeComponent ();

//
// TODO: add Any constructor code after InitializeComponent calls
//
}

/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void Dispose (bool disposing)
{
If (disposing)
{
If (components! = Null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}

# Region code generated by Windows Form Designer
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void InitializeComponent ()
{
This. listBox1 = new System. Windows. Forms. ListBox ();
This. listBox2 = new System. Windows. Forms. ListBox ();
This. SuspendLayout ();
//
// ListBox1
//
This. listBox1.ItemHeight = 12;
This. listBox1.Location = new System. Drawing. Point (32, 24 );
This. listBox1.Name = "listBox1 ";
This. listBox1.Size = new System. Drawing. Size (120,280 );
This. listBox1.TabIndex = 0;
This. listBox1.MouseDown + = new System. Windows. Forms. MouseEventHandler (this. listbox#mousedown );
//
// ListBox2
//
This. listBox2.ItemHeight = 12;
This. listBox2.Location = new System. Drawing. Point (248, 24 );
This. listBox2.Name = "listBox2 ";
This. listBox2.Size = new System. Drawing. Size (120,280 );
This. listBox2.TabIndex = 0;
This. listBox2.DragDrop + = new System. Windows. Forms. DragEventHandler (this. listBox2_DragDrop );
This. listBox2.DragEnter + = new System. Windows. Forms. DragEventHandler (this. listBox2_DragEnter );
//
// Form1
//
This. AutoScaleBaseSize = new System. Drawing. Size (6, 14 );
This. ClientSize = new System. Drawing. Size (408,333 );
This. Controls. Add (this. listBox1 );
This. Controls. Add (this. listBox2 );
This. Name = "Form1 ";
This. Text = "Form1 ";
This. Load + = new System. EventHandler (this. form#load );
This. ResumeLayout (false );

}
# Endregion

/// <Summary>
/// Main entry point of the application.
/// </Summary>
[STAThread]
Static void Main ()
{
Application. Run (new Form1 ());
}

Private void Form1_Load (object sender, System. EventArgs e)
{
This. listBox1.AllowDrop = true;
This. listBox2.AllowDrop = true;
This. listBox1.Items. Add ("");
This. listBox1.Items. Add ("B ");
This. listBox1.Items. Add ("c ");
}

Private void listbox#mousedown (object sender, System. Windows. Forms. MouseEventArgs e)
{
This. listBox1.DoDragDrop (this. listBox1.Items [this. listBox1.SelectedIndex], DragDropEffects. Move );
}

Private void listBox2_DragEnter (object sender, System. Windows. Forms. DragEventArgs e)
{
If (e. Data. GetDataPresent ("Text "))
{
E. Effect = DragDropEffects. Move;
}
}

Private void listBox2_DragDrop (object sender, System. Windows. Forms. DragEventArgs e)
{
This. listBox2.Items. Add (e. Data. GetData ("Text "));
This. listBox1.Items. Remove (e. Data. GetData ("Text "));
}
}
}

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.

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, the bitwise operator can also be used, pass in any of the members 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

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.