Accepting drops from other controls

SyntaxEditor for Windows Forms Forum

Posted 17 years ago by Kasper
Version: 4.0.0238
Avatar
Hi,

I would like to be able to drop items from another control, e.g. a ListView control, on the SyntaxEditor, and then do stuff with the dropped item. I have implemented file dropping as described in the documentation, but I simply can't figure out how to do it with controls dragged from inside the application. It seems like the SyntaxEditor refuses my drag no matter what. Could you please tell me which events I should use and which settings, to allow e.g. ListView items to be dragged to and used by the SyntaxEditor? :)

Comments (8)

Posted 17 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
It should work if you set AllowDrop on and follow the information in the "SyntaxEditor Editing Features / Custom DataObject Support" topic.


Actipro Software Support

Posted 17 years ago by Kasper
Avatar
Okay, I got it working now. Thanks :)

However, I do see one problem. Even though the cursor moves when I drag the item, the content I insert is still inserted in the old cursor position, that is, where the cursor was before the drag started. I set e.Text to null when it's a drop, because I need to call some dialogs which will insert the text in various ways. Could I update the cursor position before calling the dialog or something like that? I see that e.DragEventArgs has an X and Y property, but I'm not sure how to convert it to a cursor position, or whether or not it would actually work.
Posted 17 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
This should be handled automatically for you. All the tests I do have the text being drops at the target, where the caret moves to. For instance, load the SDI Editor and drag/drop a Windows Explorer file onto the editor. It goes where you move the mouse to.

If you are seeing something different then you might have other code that is interfering somehow. If you think it is a bug in our code that shows up in some situation, please make a small sample that shows it and send it over. Thanks.


Actipro Software Support

Posted 17 years ago by Kasper
Avatar
>This should be handled automatically for you. All the tests I do have the text being drops at the target, where the caret moves to. For instance, load the SDI Editor and drag/drop a Windows Explorer file onto the editor. It goes where you move the mouse to.

Yes, because the e.Text is used. However, as I stated in my previous post, I can't very well use this, which is why I set it to null :). The dialog I call will insert the text in one way or another at the current cursor position. To see my problem, try changing the following code to the SDI demo app. Of course, these lines won't make much sense, but they will show the problem I'm talking about :)

// Simply insert the path of the file
e.Text = filename;
if(e.Source == PasteDragDropSource.DragDrop)
{
    e.Text = null;
    editor.SelectedView.InsertSurroundingText("test", "test");
}
Posted 17 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Stick this inside your if block:
editor.Caret.Offset = editor.SelectedView.LocationToOffset(editor.PointToClient(new Point(e.DragEventArgs.X, e.DragEventArgs.Y)), LocationToPositionAlgorithm.BestFit);
To make it more robust in multi-view scenarios you should also check to see which EditorView the mouse is over and use that view instead of the SelectedView.


Actipro Software Support

Posted 17 years ago by Peter Chapman
Avatar
I've come across the same problem with the InsertText as alluded to in a previous post on this thread.

Steps to repeat:

1) type a line of text in the control.
2) move the caret using the arrow buttons to (say) offset 5.
3) Use the drag and drop functionality, drag over an object and watch the caret move to (say) offset 15 and drop there.
4) In the drop handler code, call something like:

syntaxEditor.Document.InsertText(DocumentModificationType.Custom, syntaxEditor.Caret.Offset, text);

5) The insert will be at offset 5, because syntaxEditor.Caret.Offset is 5 and not 15.

My real world problem is obviously more complicated, and setting e.text to the text to insert is not feasible. Also, the SelectedView.LocationToOffset workaround is also not useable, unless i start passing the coordinates around, which is a big ugly.

I'll send a sample to support.
Posted 17 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Pete,

In your sample form, just change the DragDrop part of the PasteDragDrop to this and it works fine:
e.Text = null;
syntaxEditor1.Caret.Offset = syntaxEditor1.SelectedView.LocationToOffset(syntaxEditor1.PointToClient(new Point(e.DragEventArgs.X, e.DragEventArgs.Y)), LocationToPositionAlgorithm.BestFit);
ProcessDrop("<DROP>");
Keep in mind that we are tracking the coordinates for you so it's simply a matter of converting the mouse position to a character offset and moving the caret there before manually inserting text.


Actipro Software Support

Posted 17 years ago by Peter Chapman
Avatar
Yep, that's fixed it, thanks.

Great product BTW.
The latest build of this product (v24.1.0) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.