How do I enable the single link between the nodes?
In WinForms Diagram, we can eliminate creating two links for the same two symbols by checking the link nodes' FromNode and ToNode properties. While creating lines, we have to check the endpoints. If the endpoints already exist with the symbols, then we have to remove the link. While creating links, we can get the link endpoints using the ConnectionsChanged event.
C#
// Adding ConnectionChanged event
((DocumentEventSink)model1.EventSink).ConnectionsChanged += new CollectionExEventHandler(MainForm_ConnectionsChanged);
// Event
void MainForm_ConnectionsChanged(CollectionExEventArgs evtArgs)
{
if (evtArgs.ChangeType == CollectionExChangeType.Insert)
{
foreach (Node n1 in this.diagram1.Model.Nodes)
{
Line tl = n1 as Line;
if (n1 is Line)
{
Line lc1 = n1 as Line;
foreach (Node n in this.diagram1.Model.Nodes)
{
if (n is Line && n != n1)
{
Line lc = n as Line;
if (((lc1.FromNode == lc.FromNode) && (lc1.ToNode == lc.ToNode)) || ((lc1.ToNode == lc.FromNode) && (lc1.FromNode == lc.ToNode)))
{
this.diagram1.Model.RemoveChild(lc1);
MessageBox.Show("Already, a link has been created for the symbols.");
}
}
}
}
}
}
}
VB
' Adding ConnectionChanged event
AddHandler (CType(model1.EventSink, DocumentEventSink)).ConnectionsChanged, AddressOf MainForm_ConnectionsChanged
' Event
Sub MainForm_ConnectionsChanged(evtArgs As CollectionExEventArgs)
If evtArgs.ChangeType = CollectionExChangeType.Insert Then
For Each n1 As Node In Me.diagram1.Model.Nodes
Dim tl As Line = TryCast(n1, Line)
If TypeOf n1 Is Line Then
Dim lc1 As Line = TryCast(n1, Line)
For Each n As Node In Me.diagram1.Model.Nodes
If TypeOf n Is Line AndAlso Not n Is n1 Then
Dim lc As Line = TryCast(n, Line)
If ((lc1.FromNode = lc.FromNode) AndAlso (lc1.ToNode = lc.ToNode)) OrElse ((lc1.ToNode = lc.FromNode) AndAlso (lc1.FromNode = lc.ToNode)) Then
Me.diagram1.Model.RemoveChild(lc1)
MessageBox.Show("Already, a link has been created for the symbols.")
End If
End If
Next n
End If
Next n1
End If
End Sub
Conclusion
I hope you enjoyed learning about how to enable the single link between the nodes.
You can refer to our WinForms Diagram feature tour page to learn about its other groundbreaking feature representations. You can also explore our WinForms Diagram documentation to understand how to present and manipulate data.
For current customers, you can check out our WinForms 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 WinForms Diagram and other WinForms components.
If you have any queries or require clarifications, please let us know in the comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!