Make the port of a shape to only accept one connection

Applies to: Nevron Diagram for .NET

How to make the port of a shape to only accept one connection?

To make the port of a shape to only accept one connection, you can limit the number of plugs that the port will allow. The diagram will raise the Connecting event of the document event sink service, whenever a port is connected to a plug. Cancelling this event will effectively prohibit the connection.

For example, the following code will allow only one connection to the top port of the shape:

[C#]
document.EventSinkService.Connecting += new ConnectionCancelEventHandler(OnConnecting);
  
void OnConnecting(NConnectionCancelEventArgs args)
{
    // get the port in the connection
    NPort port = document.GetElementFromUniqueId(args.UniqueId1) as NPort;
    if (port == null)
    {
        port = document.GetElementFromUniqueId(args.UniqueId2) as NPort;
        if (port == null)
        return;
    }
      
    // typically the top port has the "Top" name
    if (port.Name != "Top")
    return;
      
    // if the port is already connected to at least one plug -> cancel the connection
    if (port.Plugs.Count < 1)
    return;
      
    args.Cancel = true;
    return;
}

[VB.NET]
AddHandler document.EventSinkService.Connecting, AddressOf OnConnecting
   
Private Sub OnConnecting(args As NConnectionCancelEventArgs)
    ' get the port in the connection
    Dim port As NPort = TryCast(document.GetElementFromUniqueId(args.UniqueId1), NPort)
    If port Is Nothing Then
        port = TryCast(document.GetElementFromUniqueId(args.UniqueId2), NPort)
        If port Is Nothing Then
            Return
        End If
    End If
   
    ' typically the top port has the “Top” name
    If port.Name <> Top Then
        Return
    End If
   
    ' if the port is already connected to at least one plug -> cancel the connection
    If port.Plugs.Count < 1 Then
        Return
    End If
   
    args.Cancel = True
    Return
End Sub

Article ID: 23, Created On: 9/29/2010, Modified: 5/5/2011