Documentation
Activity
Sample Code available – Check the "ClipboardSample" Sample
Documentation
Activity
Sample Code available – Check the “DragDropSample” Sample
-
Create a new project with the name “DragDropSample”
-
Handle the
MouseDown
event for theTextBox
as follows:textBox1.DoDragDrop(textBox1.Text, DragDropEffects.Copy);
-
Handle the
DragEnter
event for theListView
as follows:// Display some information about the DragDrop information in the // richTextBox1 control to show some of the information available. richTextBox1.Text = "Allowed Effect: " + e.AllowedEffect + "\r\nAvailable Formats:\r\n"; // Data may be available in more than one format, so loop through // all available formats and display them in richTextBox1. foreach (string availableFormat in e.Data.GetFormats(true)) { richTextBox1.Text += "\t" + availableFormat + "\r\n"; } // This control will use any dropped data to add items to the listbox. // Therefore, only data in a text format will be allowed. Setting the // autoConvert parameter to true specifies that any data that can be // converted to a text format is also acceptable. if (e.Data.GetDataPresent(DataFormats.Text, true)) { // Some controls in this sample allow both Copy and Move effects. // If a Move effect is allowed, this implementation assumes a Move // effect unless the CTRL key was pressed, in which case a Copy // effect is assumed. This follows standard DragDrop conventions. if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move && (e.KeyState & CtrlKey) != CtrlKey) { // Show the standard Move icon. e.Effect = DragDropEffects.Move; } else { // Show the standard Copy icon. e.Effect = DragDropEffects.Copy; } }
-
Handle the
DragDrop
event for theListView
as follows:/// <summary> /// The DragDrop event of the target control fires when a drop actually occurs over /// the target control. This is where the data being dragged is actually processed. /// /// This event will fire only if the AllowDrop property of the target control has /// been set to true. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">A DragEventArgs that contains the event data.</param> private void listBox1_DragDrop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.Text, true)) { // Create the list item using the data provided by the source control. listBox1.Items.Add(e.Data.GetData(DataFormats.Text)); } }