Applies to: Nevron Diagram for .NET

How to connect two shapes using specific ports?

To connect two shapes using specific ports, you can use the following example:

[C#]
// Create a shape factory
NBasicShapesFactory f = new NBasicShapesFactory(document);
f.DefaultSize = new NSizeF(100, 60);
  
// Create 2 rectangle shapes
NShape rect1 = f.CreateShape(BasicShapes.Rectangle);
rect1.Location = new NPointF(0, 0);
document.ActiveLayer.AddChild(rect1);
  
NShape rect2 = f.CreateShape(BasicShapes.Rectangle);
rect2.Location = new NPointF(200, 100);
document.ActiveLayer.AddChild(rect2);
  
// Create a line shape that will be used to connect them
NLineShape line = new NLineShape();
document.ActiveLayer.AddChild(line);
  
// Get the ports you want to connect
NPort port1 = (NPort)rect1.Ports.GetChildByName("Bottom");
NPort port2 = (NPort)rect2.Ports.GetChildByName("Top");
  
// Connect the ports to the start and end plug of the line
line.StartPlug.Connect(port1);
line.EndPlug.Connect(port2);

[VB.NET]
' Create a shape factory 
Dim f As New NBasicShapesFactory(document)
f.DefaultSize = New NSizeF(100, 60)
  
' Create 2 rectangle shapes 
Dim rect1 As NShape = f.CreateShape(BasicShapes.Rectangle)
rect1.Location = New NPointF(0, 0)
document.ActiveLayer.AddChild(rect1)
  
Dim rect2 As NShape = f.CreateShape(BasicShapes.Rectangle)
rect2.Location = New NPointF(200, 100)
document.ActiveLayer.AddChild(rect2)
  
' Create a line shape that will be used to connect them 
Dim line As New NLineShape()
document.ActiveLayer.AddChild(line)
  
' Get the ports you want to connect 
Dim port1 As NPort = DirectCast(rect1.Ports.GetChildByName("Bottom"), NPort)
Dim port2 As NPort = DirectCast(rect2.Ports.GetChildByName("Top"), NPort)
  
' Connect the ports to the start and end plug of the line 
line.StartPlug.Connect(port1)
line.EndPlug.Connect(port2)

Article ID: 7, Created On: 9/28/2010, Modified: 11/15/2010