SyntaxEditor supports drag and drop operations within itself, as well as with external controls.
Tip
Drag and Drop and Clipboard operations share many of the same concepts. Refer to the Clipboard Operations topic for details specific to working with the clipboard.
Allowing Drag and Drop
SyntaxEditor supports drag and drop by default. However, drags and drops can also be disabled if desired.
The Syntaxfalse
to disable dragging from the editor.
The SyntaxEditor.AllowDrop
property can be set to false
to disable dropping onto the editor.
Dragging with HTML and RTF Export
The text that is dragged can include HTML and RTF exported highlighting. Then, if you drop the text in an application such as Word, the text will appear with syntax highlighting.
The Syntax
The Syntax
Customizing Text to be Dragged
Sometimes it is useful to be able to customize the text, or objects, to be dragged. The Syntax
In its event arguments, it passes the IDataDataFormats.UnicodeText
and DataFormats.Text
entries based on the current selection in the editor. The IData
Note
The IDataIDataObject
. IDataIDataObject
does and provides a consistent API across our supported platforms.
Customizing Text to be Dropped
Just like the Syntax
- Paste operations
- Paste completion
- CanPaste checks
- Drag enter
- Drag over
- Drag/drop
This event passes the IDataIDataObject
(see note above) and provides access to any shared application data (such as non-text formats) that was used to trigger the event.
The Pastenull
to insert nothing.
The Text
property is auto-populated by examining the IData
DataFormats.UnicodeText
DataFormats.Text
DataFormats.StringFormat
If dragged data doesn't include one of the standard text formats, then the Text
property will be null
. The easiest way to support dragging onto SyntaxEditor is to ensure that one of the standard text formats is set when initiating the drag.
The following code demonstrates initiating a drag with a String
value that will be automatically converted to DataFormats.StringFormat
.
If more than one data format is needed, simply make sure text is one of the standard formats like shown in the following code:
See the "Customizing Drag Behavior" section below for more advanced drag scenarios like file drop.
Important
For drag-related actions, the PasteCopy
or Move
to indicate that the object can be dropped.
The Paste
Tip
The Paste
Customizing Drag Operations
The PasteDragEventArgs.Effect
property to determine the type of drag operation, if any, that should be accepted. Under default scenarios, this property is initialized based on the availability of text on the IDataDragEventArgs.Effect
property must be changed to the desired operation.
Operation | Description |
---|---|
DragDropEffects.Copy |
Indicates drag should be allowed that is a copy of the source. |
DragDropEffects.Move |
Indicates drag should be allowed that will move the text from the source to the drop location. |
DragDropEffects.None |
Indicates drag should not be allowed. |
Multiple drag-related actions are handled by the Paste
- DragEnter
- DragOver
- DragDrop
The current action can be determined by the Paste
DragEnter Action
The Paste
The value of DragEventArgs.Effect
must be set to Copy
or Move
to allow a drop; otherwise, None
if drop is not allowed.
Tip
Setting a non-default operation for this event is only necessary if the data being dragged is not automatically recognized as text or to support dropping custom data onto the view.
Important
The DragOver
and DragDrop
actions will not be raised if the DragEnter
action is completed with the operation set to None
.
DragOver Action
The PasteIEditorView
.
The value of DragEventArgs.Effect
must be set to Copy
or Move
to allow a drop at the current pointer location; otherwise, None
if drop is not allowed.
Tip
The Drag
Important
If custom logic was used to override the default operation of the DragEnter
action, the same logic will typically need to be repeated for the DragOver
action.
DragDrop Action
The Paste
The value of DragEventArgs.Effect
can be set to Copy
or Move
to allow for a default text-based drag to be completed. When set to Copy
or Move
, the value of the PasteText
property is null
, a Copy
or Move
operation will have no effect. Set the operation to None
to prevent any default handling of text-based drag operations.
Important
If custom logic is used to handle the drop action, the operation must be set to None
to prevent any default handling of text-based data that may have also been available with the drag event. Even when custom data is expected, a fallback text-based format of the data may have also been present which would be dropped onto the view if not canceled.
File Drop Example
Dragging a file from the file system and onto the editor (i.e., DataFormats.FileDrop
) is a good example of a scenario which requires customizing the drag operations.
The following code illustrates checking for file drop and configuring the editor to allow the drag:
Reselection of Text After a Drop
By default, any dropped text will be reselected following the drop. The Syntaxfalse
to disable this behavior.