RitchTextBox Drag & Drop from RTF and HTML Originators

RitchTextBox Drag & Drop from RTF and HTML Originators

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

ApplicationRtf

References

  1. As I web-site possessor I believe the content material here is rattling excellent , appreciate it for your hard work. You should keep it up forever! Best of luck.

  2. Finally I found amazing content on your site, keep posting like this, Its very helpfull.

  3. A lot of other folks will be benefited from your writing. Cheers!Thank you so much for your valuable information.

    • I know this is an old post. I tried finding the cornotl that you mentioned following your links but had no luck.Do you know where I can get the cornotl A Windows Forms based text editor with HTML output by Kevin DeLafieldThanks,Neil

  4. I appreciate, cause I found just what I was looking for. You’ve ended my 4 day long hunt! God Bless you man. Have a great day. Bye

    • I plan to use this control to dcmuoent various moves of the 9 men’s morris game on my homepage. First I missed a ToolbarButtons property, which allows on load to set unused commands invisible and translate the tooltips to German. Then I found thet the Leave-event is fired just once (bug) und the FocusLost is not fired when expected. I could work arround these bugs and added ToolbarButtons the now I’m happy with this solution.

  5. Awesome post man, maintain up the good function.

  6. You need to take part in a contest for among the best blogs on the web. I will recommend this web site!

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Captcha Captcha Reload