How to create a self-referencing connector to a rectangle node in WinForms Diagram?
In our WinForms Diagram control, you can create a self-referencing connector to a Rectangle node using the following steps. First, you need to add two ports to the Rectangle node. Then, create a connector that designates the Rectangle node as both the source and target, ensuring that the connector uses different source and target port points on the same node. To avoid issues with connector overlap on the node, enable the LineRoutingEnabled property. This setting helps manage the routing of the connector and prevents it from overlapping with the node itself. We have provided the code example for how to achieve this.
//Create a rectangle node
Syncfusion.Windows.Forms.Diagram.Rectangle rectangle = new Syncfusion.Windows.Forms.Diagram.Rectangle(250, 50, 100, 100);
this.diagram1.Model.AppendChild(rectangle);
//creating connection point ports.
Syncfusion.Windows.Forms.Diagram.ConnectionPoint cp = new Syncfusion.Windows.Forms.Diagram.ConnectionPoint();
Syncfusion.Windows.Forms.Diagram.ConnectionPoint cp1 = new Syncfusion.Windows.Forms.Diagram.ConnectionPoint();
//Port position
cp.Position = Syncfusion.Windows.Forms.Diagram.Position.MiddleLeft;
cp1.Position = Syncfusion.Windows.Forms.Diagram.Position.MiddleRight;
//Adding a port to the node
rectangle.Ports.Add(cp);
rectangle.Ports.Add(cp1);
//Creating connector
Syncfusion.Windows.Forms.Diagram.OrgLineConnector connector = new Syncfusion.Windows.Forms.Diagram.OrgLineConnector(new PointF(0,0), new PointF(0,0));
//specify head and tail port point to the connector
rectangle.Ports[0].TryConnect(connector.HeadEndPoint);
rectangle.Ports[1].TryConnect(connector.TailEndPoint);
//enabling routing to connector
connector.LineRoutingEnabled = true;
//adding connector into the collection.
this.diagram1.Model.AppendChild(connector);
Conclusion:
I hope you enjoyed learning about how to create a self-referencing connector to a rectangle node in WinForms Diagram
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!