While reviewing pre-sales support cases, I stumbled upon this interesting code snippet one of our sales engineers sent to a client to mimic a drag and drop feature of MS Word.

In MS Word, you can drag and drop a file into an existing position in a document instead of replacing the current content with the new document.

In TX Text Control, this can be done in a very easy way using the DragDrop event of TextControl:

private void textControl1_DragDrop(object sender, DragEventArgs e)
{
    Point posCursor = textControl1.PointToClient(new Point(e.X, e.Y));
    TXTextControl.TextChar txChar =
        textControl1.TextChars.GetItem(posCursor, true);
    textControl1.Selection.Start = txChar.Number;
    textControl1.Selection.Load(fileDragDrop.FileName,
        fileDragDrop.StreamType);
}

The TextChars.GetItem method returns the nearest input position of a given location. The location is returned from the event directly. All we need to do is to set the input position to the new calculated input position in order to load the document using the Selection object.

Drag anbd drop files in TX Text Control

This code snippet is based on the shipped sample Howto: Drag and Drop Files and uses the same FileDragDropHandler.