How to split and join the connector when dropping a node onto an existing connector?
In our WinForms Diagram control, you can split and join the connector when dropping a node onto an existing connector using the NodesCollectionChanged event and DragDrop event. We have prepared a simple sample to achieve this. In this sample, first, a boolean property is used to identify whether an element has been dropped from the symbol palette. Based on this property, in the NodesCollectionChanged event, it is determined whether the dropped node intersects with an existing connector. If an intersection is found, the connector is split at the intersection point, and the segments are adjusted to join with the newly dropped node. We have provided the code example for how to achieve this.
Code Snippet:
diagram1.DragDrop += Diagram1_DragDrop;
diagram1.EventSink.NodeCollectionChanged += EventSink_NodeCollectionChanged;
private void Diagram1_DragDrop(object sender, DragEventArgs e)
{
isDragDrop = true;
}
private void EventSink_NodeCollectionChanged(CollectionExEventArgs evtArgs)
{
if (isDragDrop)
{
isDragDrop = false;
Node droppedNode = evtArgs.Element as Node;
NodeCollection intersectingConnectors = this.GetNodesAtPoint(diagram1.Model.Nodes, droppedNode.BoundingRectangle);
if (intersectingConnectors.First != null && intersectingConnectors.First is Line)
{
Line hitConnectorLine = intersectingConnectors.First as Line;
Node hitConnectorTargetNode = hitConnectorLine.FromNode as Node;
droppedNode.CentralPort.TryConnect(hitConnectorLine.TailEndPoint);
LineConnector lineconnector = new LineConnector(new System.Drawing.PointF(10, 200), new System.Drawing.PointF(300, 250));
droppedNode.CentralPort.TryConnect(lineconnector.HeadEndPoint);
hitConnectorTargetNode.CentralPort.TryConnect(lineconnector.TailEndPoint);
this.diagram1.Model.AppendChild(lineconnector);
}
}
}
Conclusion:
I hope you enjoyed learning about how to split and join the connector when dropping a node onto an existing connector
You can refer to our WinForms Diagram feature tour page to learn about its other groundbreaking feature representations and documentation. 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!