How can I implement drag-and-drop from other controls onto the Diagram?
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 the 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
Conclusion
I hope you enjoyed learning about how you can implement drag-and-drop from other controls onto the Diagram.
You can refer to WinForms Diagram feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WinForms Diagram example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!