A commonly used way to load a file or insert images into a document is the concept of drag and drop. This sample illustrates how to implement that concept into a TX Text Control application: When the file is dragged, it is determined whether its type and format is supported by TX Text Control. When the file is dropped, the sample handles type specific operations to load or insert the document respectively image.
The source code for this example is contained in the following directories:
Open the explorer, select a file, start dragging it to the this sample application and drop it onto the TextControl.
If its format is type of
.rtf, .htm, .html, .doc, .docx, .pdf, .xml, .txt, .tx or .xlsx,
the file is loaded as document.
If the file ends with
.jpeg, .jpg, .tif, .tiff, .bmp, .gif, .png, .wmf, .emf or .svg,
the file is inserted as image.
All other formats are not supported.
The requested behavior is handled by two events: System.Windows.Forms.Control.DragEnter and System.Windows.Forms.Control.DragDrop. To enabled these events, set the TextControl AllowDrag and AllowDrop properties to true.
When handling the DragEnter event, the type and format (TXTextControl.StreamType) is determined. This information is stored in and provided by an object of type DraggedFileInfo, which is null in case the type or format is not supported.
private void TextControl_DragEnter(object sender, DragEventArgs e) {
m_dfiFileInfo = CheckDraggedFiles((string[])e.Data.GetData(DataFormats.FileDrop));
}
Private Sub TextControl_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs)
m_dfiFileInfo = CheckDraggedFiles(CType(e.Data.GetData(DataFormats.FileDrop), String()))
End Sub
The DragDrop event handler finally invokes the type specific method to load or insert the dragged file.
private void TextControl_DragDrop(object sender, DragEventArgs e) {
// Check whether the dragged file is valid (supported by TX Text Control).
if (m_dfiFileInfo != null) {
// Insert the file as document or image.
switch (m_dfiFileInfo.FileType) {
case FileType.Document:
Open(m_dfiFileInfo.FilePath, m_dfiFileInfo.StreamType);
break;
case FileType.Image:
InsertDroppedImage(m_dfiFileInfo.FilePath);
break;
}
m_dfiFileInfo = null;
}
}
Private Sub TextControl_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs)
' Check whether the dragged file is valid (supported by TX Text Control).
If m_dfiFileInfo IsNot Nothing Then
' Insert the file as document or image.
Select Case m_dfiFileInfo.FileType
Case FileType.Document
Open(m_dfiFileInfo.FilePath, m_dfiFileInfo.StreamType)
Case FileType.Image
InsertDroppedImage(m_dfiFileInfo.FilePath)
End Select
m_dfiFileInfo = Nothing
End If
End Sub