Articles in this section
Category / Section

How to restrict duplicate connection entries of a node when undoing the delete action in WinForms Diagram?

6 mins read

In the WinForms Diagram control, you can restrict duplicate connection entries of a node when undoing the delete action by handling the NodeCollectionChanging event. In this event, ensure that connections are correctly re-established using the Connect method.

The changes mentioned below ensure that connectors are properly managed when a node is removed, thereby preventing any duplication issues upon undoing the delete action. We have provided the code example for how to achieve this.

Code Snippet :

private void EventSink_NodeCollectionChanging(CollectionExEventArgs evtArgs)
{
   if (rbHealWhenChanging.Checked)
   {
       // Heal Connection when a node is removed

       if (evtArgs.ChangeType == CollectionExChangeType.Remove && evtArgs.Elements.Count == 1 && healCB.Checked)
       {
           var deleteNode = evtArgs.Element as Node;
           var edgesEntering = (deleteNode.EdgesEntering as ArrayList).ToArray();
           var edgesLeaving = (deleteNode.EdgesLeaving as ArrayList).ToArray();

           // Disconnecting the existing connection with the history manager is in a paused state,
           // as this data has already been documented internally by the history manager.

           diagram1.Model.HistoryManager.Pause();
           deleteNode.CentralPort.DisconnectAll();
           diagram1.Model.HistoryManager.Resume();

           if (edgesLeaving.Count() == 1)
           {
               var outConnector = edgesLeaving.First() as ConnectorBase;
               if (outConnector != null)
               {
                   // Saving the to-node of the out-connector and deleting the out-connector from the diagram.
                  var leavingEdgeToNode = outConnector.ToNode as Node;
                   diagram1.Model.Nodes.Remove(outConnector);
                   if (leavingEdgeToNode != null && edgesEntering.Count() > 0)
                   {
                       // Linking the in-connectors to the new to-node.
                       foreach (var inEdge in edgesEntering)
                       {
                           if (inEdge is ConnectorBase)
                           {
                               leavingEdgeToNode.CentralPort.Connect((inEdge as ConnectorBase).HeadEndPoint);
                           }
                       }
                   }
               }
           }

           else if (edgesEntering.Count() == 1)
           {
               var inConnector = edgesEntering.First() as ConnectorBase;
               if (inConnector != null)
               {
                   // Saving the from-node of the in-connector and deleting the in-connector from the diagram.
                   var enteringEdgeFromNode = inConnector.FromNode as Node;
                   diagram1.Model.Nodes.Remove(inConnector);
                   if (enteringEdgeFromNode != null && edgesLeaving.Count() > 0)
                   {
                       // Linking the out-connectors to the new from-node.

                       foreach (var outEdge in edgesLeaving)
                       {
                           if (outEdge is ConnectorBase)
                           {
                               enteringEdgeFromNode.CentralPort.Connect((outEdge as ConnectorBase).TailEndPoint);
                           }
                       }
                   }
               }
           }
       }
   }
} 

Conclusion:

I hope you enjoyed learning about How to restrict duplicate connection entries in a node when undoing the delete action in WinForms Diagram

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 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!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied