Skip to content

Latest commit

 

History

History
103 lines (88 loc) · 3.7 KB

13 - WinForms - Clipboard, Drag and Drop.md

File metadata and controls

103 lines (88 loc) · 3.7 KB

Windows Forms – Clipboard, Drag and Drop

1. Objectives

2. Clipboard

Documentation

https://docs.microsoft.com/en-us/dotnet/framework/winforms/advanced/how-to-retrieve-data-from-the-clipboard

Activity

:octocat: Sample Code available – Check the "ClipboardSample" Sample

3. Drag and Drop

Documentation

https://docs.microsoft.com/en-us/dotnet/framework/winforms/drag-and-drop-functionality-in-windows-forms

Activity

:octocat: Sample Code available – Check the “DragDropSample” Sample

  1. Create a new project with the name “DragDropSample”

  2. Create the UI shown below
    Drag and Drop

  3. Set the AllowDrop property of the ListView to True
    AllowDrop Property True

  4. Handle the MouseDown event for the TextBox as follows:

    textBox1.DoDragDrop(textBox1.Text, DragDropEffects.Copy);
  5. Handle the DragEnter event for the ListView 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;
    	}
    }
  6. Handle the DragDrop event for the ListView 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));
    	}
    }

4. Bibliography