Monday 1 August 2011

RitchTextBox Drag & Drop from RTF and HTML Originators


Drag-and-drop operations with the Windows Forms RichTextBox control are done by handling the DragEnter and DragDrop events. To enable drag operations in a RichTextBox control[1]
1. Set the AllowDrop property of the RichTextBox control to true.
2. Add handlers for both the DragEnter and DragDrop event of RichTextBox.
richTextBox.AllowDrop = true;
  richTextBox.DragEnter += new DragEventHandler(richTextBox_DragEnter);
  richTextBox.DragDrop += new DragEventHandler(richTextBox_DragDrop);
3.DragEnter event: Use a check to ensure for data being dragged is of an acceptable type (in this case - rtf and html). The DragEventArgs.Effect property can be set to any value of the DragDropEffects enumeration. In our case for both we use DragDropEffects.Copy for both and miss this part.
Additionally a check for available RTF format is carried out using IDataObject.GetFormats(Boolean)[2] method. It gets a list of all formats that data stored in this instance is associated with or can be converted to.
private void richTextBox_DragEnter(object sender, DragEventArgs e)
  {
     e.Effect = DragDropEffects.Copy;
     String[] supportedFormats = e.Data.GetFormats(true);

     if (supportedFormats != null)
     {
        List<string> sfList = new List<string>(supportedFormats);
        //--- checked for Rtf
        if (sfList.Contains(DataFormats.Rtf.ToString()))
           richTextBox.EnableAutoDragDrop = true;
        else if (sfList.Contains(DataFormats.Html.ToString()))
           richTextBox.EnableAutoDragDrop = false;
     }
  }
In case when data are associated with DataFormats.Rtf we set RichTextBox property EnableAutoDragDrop to true and do nothing in DragDrop event handler. It works very well as converting text to rtf format and more important it works when merging 2 RTF texts (plays text on a specific position in already filled RichTextBox). This functionality is handled very well from MS and I was happy to discover this. For HTML formatted text the case is not the same. Auto drag and drop insert them as plain text. This is not what we desire. We would like to have links, e-mails, images and tables to be shown in the similar and well formatted way. That's in this case EnableAutoDragDrop property is set to false.
4. Drop event: Simply do nothing if data has RTF format association. Leave the Auto Drag Drop functionality to work. For HTML format associated data I found a sample solution for HTML 2 RTF conversion[3], that is not perfect but works in my case. You have to manually delete some part of the imported text.
private void richTextBox_DragDrop(object sender, DragEventArgs e)
   {
       String[] supportedFormats = e.Data.GetFormats(true);
       string DataStr = string.Empty;
       if (supportedFormats != null)
       {
           List<string> sfList = new List<string>(supportedFormats);
          if (sfList.Contains(DataFormats.Html.ToString()))
          {
               WebBrowser webBrowser = new WebBrowser();
               webBrowser.CreateControl(); // only if needed
               DataStr = (string)e.Data.GetData(DataFormats.Html, true);
               webBrowser.DocumentText = DataStr;
               while (webBrowser.DocumentText != DataStr)
                   Application.DoEvents();
               webBrowser.Document.ExecCommand("SelectAll", false, null);
               webBrowser.Document.ExecCommand("Copy", false, null);
               richTextBox.Paste();
          }
       }
   }
5. Final step is to create an event handler for LinkClicked event to process a link that has been clicked within the control.
private void richTextBox_LinkClicked(object sender, LinkClickedEventArgs e)
 {
     System.Diagnostics.Process.Start(e.LinkText);
 }

Phraseology

  • DragDrop Event - The DragDrop event occurs when the user completes a drag-and-drop operation by dragging an object over the control and then dropping it onto the control by releasing the mouse button.
  • DragEventArgs - A DragEventArgs object specifies any data associated with this event; the current state of the SHIFT, CTRL, and ALT keys; the location of the mouse pointer; and the drag-and-drop effects allowed by the source and target of the drag event.
  • Originator - source of the drag event.

Excample

An example C# solution and more detail you can find on my personal "WordPress" installation at blog.iordanov.info. Very interesting post you can find on this site


References



No comments: