Back to Devexpress

How to: Create a Shape Connector

windowsforms-120674-controls-and-libraries-spreadsheet-examples-shapes-how-to-create-a-shape-connector.md

latest5.7 KB
Original Source

How to: Create a Shape Connector

  • Oct 29, 2020
  • 2 minutes to read

You can connect two shapes with a connector. This example illustrates how to create a shape connector, modify it, and attach both its ends to shapes.

Create a Shape Connector

Call the ShapeCollection.AddConnector method to create an unbound shape connector.

csharp
Shape shapeConnector = worksheet.Shapes.AddConnector(ConnectorType.Curved, 10, 10, 500, 100);
vb
Dim shapeConnector As Shape = worksheet.Shapes.AddConnector(ConnectorType.Curved, 10, 10, 500, 500)

Modify a Shape Connector

The table below lists an API used to change the connector’s appearance.

|

Task

|

API

| | --- | --- | |

Change the connector’s type

|

ConnectorFormat.ConnectorType

| |

Change the connector’s color and thickness

|

ShapeFormatBase.Outline

ShapeOutline.Width

| |

Specify arrowhead options for the connector’s start

|

ArrowSettings.StartArrowheadType

ArrowSettings.StartArrowheadWidth

ArrowSettings.StartArrowheadLength

| |

Specify arrowhead options for the connector’s end

|

ArrowSettings.EndArrowheadType

ArrowSettings.EndArrowheadWidth

ArrowSettings.EndArrowheadLength

|

The code sample below shows how to use these members to make the connector look as it does on the following image.

csharp
// Change the connector's color and thickness.
shapeConnector.Outline.SetSolidFill(Color.Black);
shapeConnector.Outline.Width = 2.5;

// Specify arrowhead options for the connector's ends.
ConnectorFormat connectorFormat = shapeConnector.ConnectorFormat;
connectorFormat.Arrows.StartArrowheadType = ArrowheadType.Stealth;
connectorFormat.Arrows.StartArrowheadWidth = ArrowheadSize.Large;
connectorFormat.Arrows.StartArrowheadLength = ArrowheadSize.Small;

connectorFormat.Arrows.EndArrowheadType = ArrowheadType.Stealth;
connectorFormat.Arrows.EndArrowheadWidth = ArrowheadSize.Large;
connectorFormat.Arrows.EndArrowheadLength = ArrowheadSize.Small;
vb
' Change the connector's color and thickness.
shapeConnector.Outline.SetSolidFill(Color.Black)
shapeConnector.Outline.Width = 2.5

' Specify arrowhead options for the connector's ends.
Dim connectorFormat As ConnectorFormat = shapeConnector.ConnectorFormat
connectorFormat.Arrows.StartArrowheadType = ArrowheadType.Stealth
connectorFormat.Arrows.StartArrowheadWidth = ArrowheadSize.Large
connectorFormat.Arrows.StartArrowheadLength = ArrowheadSize.Small

connectorFormat.Arrows.EndArrowheadType = ArrowheadType.Stealth
connectorFormat.Arrows.EndArrowheadWidth = ArrowheadSize.Large
connectorFormat.Arrows.EndArrowheadLength = ArrowheadSize.Small

Attach Shapes to the Connector

The following methods allow you to link shapes to a connector.

Check the ShapeGeometry.ConnectionSiteCount property to retrieve the shape’s connection sites’ number. The sites’ index order is different for each shape. Generally, the index begins with the top side (0) and grows counterclockwise, as shown below.

As for the 3-D shapes, the connection sites’ index changes as follows.

The code sample shows how to attach shapes to the connector. The connector is bound to the third site of each shape.

csharp
connectorFormat.ConnectStartPoint(shape1, 3);
connectorFormat.ConnectEndPoint(shape2, 3);
vb
connectorFormat.ConnectStartPoint(shape1, 3)
connectorFormat.ConnectEndPoint(shape2, 3)

Tip

Call the ConnectorFormat.DisconnectStartPoint or ConnectorFormat.DisconnectEndPoint method to detach the connector from shapes.