This is a migrated thread and some comments may be shown as answers.

Drag-And-Drop from RadTextBox to another [external] control

1 Answer 59 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Charles Willwerth
Top achievements
Rank 1
Charles Willwerth asked on 01 May 2012, 04:31 PM
I want to drag element(s) from the RadTextBox to another control. Can you provide me an example as I have been unsuccessful making this work?

To be clear, I understand how to do the reverse--as shown here. But that is not what I want. I want to drag information from the RadTextBox, as a copy and not a move, and then place that data into another control. I am pretty flexible if there are some limitations so dragging an image or text is fine. I have tried working with RadDragAndDropManager.AddDragInfoHandler and RadDragAndDropManager.AddDragQueryHandler with no success.

Much thanks!


1 Answer, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 07 May 2012, 01:21 PM
Hello Charles,

We had not encountered your scenario till now. Unfortunately, you cannot use RadDragAndDropManager to drop items from the RadRichTextBox to other controls although the reverse scenario works fine. The reason for this is that mouse events are handled by the RRTB and don't reach the manager. However, you can try to implement your own logic for drag and drop. First of all you need to cancel the MoveSelectionCommand so that the selection would not be moved (as in cut), but copied. 

void radRichTextBox_CommandExecuting(object sender, Telerik.Windows.Documents.RichTextBoxCommands.CommandExecutingEventArgs e)
       {
           if (e.Command is MoveSelectionCommand)
           {
               e.Cancel = true;
           }
       }

Then you can attach to a mouse event of the desired control and having successfully found the proper place to insert text, get the selected text or DocumentFragment (basically a list of all selected document elements, paragraphs, images, etc.) obtained by one of the following two public methods:

this.radRichTextBox.Document.Selection.CopySelectedDocumentElements()
this.radRichTextBox.Document.Selection.GetSelectedText()

The first one will get you text, images, etc. while the second will contain only text. If you want to insert images and other rich text, you should use the CopySelectedDocumentElements method and traverse through the document fragment, filtering the custom information you need to insert into the other control. If you encounter difficulties, contact us back.

All the best, Martin
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
RichTextBox
Asked by
Charles Willwerth
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Share this question
or