Windows Forms controls implement drag and drop content (DragDrop) function

First, the control content onto other controls

  In the development process, often have such a requirement, a drag control data to control another. For example, wherein a data ListBox onto another in the ListBox. Or drag DataGridView data to a node in the TreeView.  

  In the application, by processing a series of events, such as DragEnter, DragLeave and DragDrop events to implement drag and drop in Windows applications operations. By using the available information about these events parameters, you can easily drag and drop.

  Drag and drop operation is in the code through the three steps, the first is to start drag and drop operation to achieve the MouseDown event response code on the control and drag the data needed, and calls DoDragDrop () method; followed by drag and drop effects in the target Add DragEnter event response code, and the like using DragDropEffects enumerated type copy or move a drag effect on the control; last operation is to place the data, add DragDrop response control code on the target, to add data to the control target.

private void Form1_Load(object sender, System.EventArgs e)
{
    this.listBox1.AllowDrop = true;
    this.listBox2.AllowDrop = true;
    this.listBox1.Items.Add("a");
    this.listBox1.Items.Add("b");
    this.listBox1.Items.Add("c");
}

private void listBox1_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(DataFormats.Text))
    {
        e.Effect = DragDropEffects.Move;
    }
}

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

Second, drag the file to control access to documents path

  Drag and drop the file or directory to your program, that the user experience is good.

  Get dragged over the path of the code: (System.Array) e.Data.GetData (DataFormats.FileDrop).

  Then you can copy and paste these paths.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        SetCtrlDrag.SetCtrlDragEvent(this.textBox1);
    }
}

public class SetCtrlDrag
{
    public static void SetCtrlDragEvent(Control ctrl)
    {
        if (ctrl is TextBox)
        {
            TextBox tb = ctrl asThe TextBox; 
            tb.AllowDrop = to true ; 
            tb.DragEnter + = (SENDER, E) => 
            { 
                e.Effect = DragDropEffects.Link; // icon drag 
                 };

            tb.DragDrop += (sender, e) =>
            {
                ((TextBox)sender).Text = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            };
        }
    }
}

Interface effects

TestFilePathDrag

Third, the instructions

msdn:http://msdn.microsoft.com/zh-cn/library/system.windows.forms.dragdropeffects.aspx

1. method

When drag and drop effects, C # provides a systematic method DoDragDrop method for implementing drag and drop operation began, which is defined by the Control class, directly or indirectly, since the controls are derived from the Control class, the developer can in any the method calls DoDragDrop visualization component. DoDragDrop method uses the following syntax:

public DragDropEffects DoDragDrop ( Object data,DragDropEffects allowedEffects)

  data: the user to drag data content. To be dragged content must be passed to the first argument of this method position.

  allowedEffects: One DragDropEffects enumerated values, the effect of this type comprises a drag operation. DragDropEffects enumeration values ​​shown in Table 32.8.

  DragDropEffects sheet 举值:

  •     All: Copy from the drag source, the data is removed, and scroll to the drop target
  •     Copy: copy the data into the drop target
  •     Link: the drag link the data source to the drop target
  •     Move: drag the mobile data source to the drop target
  •     None: the drop target does not accept the data
  •     Scroll: about to start rolling in the drop target, or is currently rolling

  When using the developer DoDragDrop method, must specify the parameters allowedEffects any member of a table. In addition, bitwise operators may be used, wherein any one of the member as a complete set of parameters passed, to give the desired drag effect to achieve the key code is as follows:

DragDropEffects.Copy| DragDropEffects.None

2. event

  C # provides a drag and drop event system, used in conjunction with drag and drop method to achieve better results. Conventional drag events as shown in Table.

Event on the target:

  •     DragEnter: When users drag and drop operation for the first time during the mouse cursor onto the controls, will lead the event
  •     DragOver: If the mouse moves but stays within the same control, the event trigger DragOver
  •     DragDrop: occurs when the drag operation is completed
  •     DragLeave: If the user moves out of a window, the event triggered DragLeave

Event on the source:

  •     GiveFeedback: Occurs during a drag operation
  •     QueryContinueDrag: During the drag and drop operation, when the keyboard or mouse button state changes will lead to QueryContinueDrag event. QueryContinueDrag event allows the drag source to determine whether the drop operation should be canceled

Guess you like

Origin www.cnblogs.com/springsnow/p/11586293.html