DragDrop File (not PasteDragDrop)

SyntaxEditor for Windows Forms Forum

Posted 12 years ago by Radjesh Klauke - X-impress
Version: 12.1.0300
Avatar

Hi,

 

I'm trying to implement the DragDrop, but I can't seem to getting it to work. My code in the myEditor_dragdrop event. Allowdrop is enabled.

If e.Data.GetDataPresent(DataFormats.FileDrop) Then
   Dim dropFiles As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())

   '   check the extension of the file
   If Path.GetExtension(dropFiles(0)).ToLower = ".txt" Then
      lst_xsyn.Text = File.ReadAllText(dropFiles(0).ToString)

      '   enable the start
      tbar_btn_conv.Enabled = True
   End If
End If

 When I drag the file nothing happens. The code works in the pastedrop-event. Am I doing anything wrong here?

Comments (9)

Posted 12 years ago by Tobias Lingemann - Software Devolpment Engineer, Vector Informatik GmbH
Avatar

I think you have to use the PasteDragDrop event.

The drag and drop event handler from the WinForms control are overridden, so you don't have much choice.

Is there any reason, why you can't use the PasteDragDrop event?


Best regards, Tobias Lingemann.

Posted 12 years ago by Radjesh Klauke - X-impress
Avatar

Let's say I'm draggin a textfile to the editor. but it is the wrong one. The PasteDragDrop immediately pastes the text into the editor. I can't reconsider and I can't use the undo.

[Modified 12 years ago]

Answer - Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Radjesh,

Sorry but we handle all the WinForms drag and drop events internally.  This is required so that we can support drag and drop within the text properly, such as being able to move a "drop target" caret around, etc.  You'll have to use the events we expose to achieve dropping.

You can set the PasteDragDrop event's e.Text value to anything you want to customize what is inserted.  Set it to null to cancel the drop.


Actipro Software Support

Posted 12 years ago by Tobias Lingemann - Software Devolpment Engineer, Vector Informatik GmbH
Avatar

Hi Radjesh,

The text is not pasted immediatly. You need to look at the event args property "Source". This indicates if the event is currently in DragEnter or DragDrop mode.

if (e.Source == PasteDragDropSource.DragEnter)
{
  // analyze text and cancel or modify it
}
else if (e.Source == PasteDragDropSource.DragDrop)
{
  // drop the text
}


Best regards, Tobias Lingemann.

Posted 12 years ago by Radjesh Klauke - X-impress
Avatar
PasteDragDrop - Event

If e.DataObject.GetDataPresent(DataFormats.FileDrop) Then
            Dim dropFiles As String() = CType(e.DataObject.GetData(DataFormats.FileDrop), String())

   If Path.GetExtension(dropFiles(0)).ToLower = ".txt" Then
      If e.Source = PasteDragDropSource.DragDrop Then
         If e.DataObject.GetDataPresent(DataFormats.FileDrop) Then
            e.DragEventArgs.Effect = DragDropEffects.Copy
         End If

         testeditor.Text = File.ReadAllText(dropFiles(0).ToString)
       End If
   End If
End If

 nothing happens.... Also don't see the dragdropeffects. Thanks for the reply btw.

Posted 12 years ago by Tobias Lingemann - Software Devolpment Engineer, Vector Informatik GmbH
Avatar

I think you need to handle the DragEnter event (means here to check e.Source for DragEnter). There you need to accept the item. You can do so by setting the e.Text value to String.Empty (or any value except null) and set the allowed effects.

If the item is accepted the DragDrop event will be hit, otherwise you wont even get there. And that is what you have now.

Just for you to explain. That is pretty similiar to the default WinForms behavior. The only difference is that all events are handled in one single handler.


Best regards, Tobias Lingemann.

Posted 12 years ago by Radjesh Klauke - X-impress
Avatar

Yes, the dragenter works. Already tested it. But as soon as I drag a textfile above it the text is pasted immediately. How do I only "paste" the text on the "mouse release"?

If e.Source = PasteDragDropSource.DragEnter Then
Answer - Posted 12 years ago by Tobias Lingemann - Software Devolpment Engineer, Vector Informatik GmbH
Avatar

That is weired. I currently dont have the time to test your code, but thats what I do for normal objects:

      if (e.Source == PasteDragDropSource.DragEnter)
      {
        e.DragEventArgs.Effect = DragDropEffects.Copy;
        // Note: set text not to null, because otherwise the operation will not be allowed
        e.Text = String.Empty;
      }
      else if (e.Source == PasteDragDropSource.DragDrop)
      {
        e.DragEventArgs.Effect = DragDropEffects.Copy;
        // Note: set text to null, because otherwise text from clipboard is used if text is String.Empty?!?
        e.Text = "text for my object";
      }

Note that you dont need to read the file during DragEnter. I dont think it is a problem either, but if you load the file twice you might get performance issues.


Best regards, Tobias Lingemann.

Posted 12 years ago by Radjesh Klauke - X-impress
Avatar

Thanks.

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.