We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How can I implement drag-and-drop from other controls onto the Diagram?

Platform: WinForms |
Control: Diagram |
Published Date: April 30, 2011 |
Last Revised Date: June 4, 2019

How can I implement drag-and-drop from other controls onto the Diagram?

The sample included in this KnowledgeBase demonstrates how you can implement drag and drop support in an Essential Diagram application.

  • An image from a Windows Forms PictureBox control can be dragged and dropped onto the Diagram. A node of type BitmapNode is created.
  • A node from an Essential Tools' TreeViewAdv control can be dragged and dropped onto the Diagram. A node of type TextNode is created.
    Note:

    This sample uses the TreeViewAdv control from Essential tools.

 

C#

private void diagram1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
  {
   //Allow Drop Cursor only if it is a Diagram Node or a Bitmap
   if ( (e.Data.GetDataPresent(typeof(NodeCollection))) | (e.Data.GetDataPresent(DataFormats.Bitmap))|(e.Data.GetDataPresent(typeof(TreeNodeAdv))))
   {
    e.Effect = DragDropEffects.All;
   }
   else
    e.Effect = DragDropEffects.None;
  }
   private void diagram1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
  {
   Point pt = this.diagram1.PointToClient(new Point(e.X, e.Y));
   //if this is a bitmap then insert a BitmapNode in Diagram
   if (e.Data.GetDataPresent(DataFormats.Bitmap))
   {
    BitmapNode bmpnode = new BitmapNode((Bitmap)e.Data.GetData(DataFormats.Bitmap));
    InsertNodesCmd insCmd = new InsertNodesCmd( this.diagram1.Model, bmpnode, this.diagram1.View.DeviceToView( pt ) );
diagram1.Controller.ExecuteCommand(insCmd);
   }
   else if(e.Data.GetDataPresent(typeof(TreeNodeAdv)))
   {
    TreeNodeAdv treenode = (TreeNodeAdv) e.Data.GetData(typeof(TreeNodeAdv));
    TextNode textnode = new TextNode();
    textnode.Text = treenode.Text;
    textnode.SizeToText(new SizeF(0,0));
    textnode.Location = this.diagram1.View.DeviceToView( pt );
this.diagram1.Model.AppendChild(textnode);
    this.diagram1.Refresh();
   }
  }
 

 

VB

Private Sub diagram1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles diagram1.DragEnter
'Allow Drop Cursor only if it is a Diagram Node or a Bitmap
If e.Data.GetDataPresent(GetType(NodeCollection)) Or e.Data.GetDataPresent(DataFormats.Bitmap) Or e.Data.GetDataPresent(GetType(TreeNodeAdv)) Then
e.Effect = DragDropEffects.All
Else
e.Effect = DragDropEffects.None
End If
End Sub 'diagram1_DragEnter
Private Sub diagram1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles diagram1.DragDrop
Dim pt As Point = Me.diagram1.PointToClient(New Point(e.X, e.Y))
'if this is a bitmap then insert a BitmapNode in Diagram
If e.Data.GetDataPresent(DataFormats.Bitmap) Then
Dim bmpnode As BitmapNode = New BitmapNode(CType(e.Data.GetData(DataFormats.Bitmap), Bitmap))
Dim insCmd As InsertNodesCmd = New InsertNodesCmd(Me.diagram1.Model, bmpnode, Me.diagram1.View.DeviceToView(pt))
diagram1.Controller.ExecuteCommand(insCmd)
ElseIf e.Data.GetDataPresent(GetType(TreeNodeAdv)) Then
Dim treenode As TreeNodeAdv = CType(e.Data.GetData(GetType(TreeNodeAdv)), TreeNodeAdv)
Dim textnode As TextNode = New TextNode
textnode.Text = treenode.Text
textnode.SizeToText(New SizeF(0, 0))
textnode.Location = Me.diagram1.View.DeviceToView(pt)
Me.diagram1.Model.AppendChild(textnode)
Me.diagram1.Refresh()
End If
End Sub 'diagram1_DragDrop
 

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon