Back to Devexpress

How to: Create a Shape

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

latest5.9 KB
Original Source

How to: Create a Shape

  • Oct 29, 2020
  • 3 minutes to read

This topic describes how to create a shape and change its form, size, and rotation angle.

Create a Shape

The example below shows how to use the ShapeCollection.AddShape method overloads to insert shapes into a worksheet.

csharp
// Set the measurement unit to Inches.
workbook.Unit = DevExpress.Office.DocumentUnit.Inch;

// Insert a shape at the specified position in a worksheet.
Shape shape1 = worksheet.Shapes.AddShape(ShapeGeometryPreset.RectangularCallout, 0.68f, 0.22f, 2f, 1.25f);

// Insert a shape so that its top left corner is in the specified cell.
Shape shape2 = worksheet.Shapes.AddShape(ShapeGeometryPreset.RoundedRectangularCallout, worksheet.Cells["F2"], 2f, 1.25f);

// Insert a shape that fits into the specified cell range.
Shape shape3 = worksheet.Shapes.AddShape(ShapeGeometryPreset.OvalCallout, worksheet["J2:L7"]);
vb
' Set the measurement unit to Inches.
workbook.Unit = DevExpress.Office.DocumentUnit.Inch

' Insert a shape at the specified position in a worksheet.
Dim shape1 As Shape = worksheet.Shapes.AddShape(ShapeGeometryPreset.RectangularCallout, 0.68F, 0.22F, 2F, 1.25F)

' Insert a shape so that its top left corner is in the specified cell.
Dim shape2 As Shape = worksheet.Shapes.AddShape(ShapeGeometryPreset.RoundedRectangularCallout, worksheet.Cells("F2"), 2F, 1.25F)

' Insert a shape that fits into the specified cell range.
Dim shape3 As Shape = worksheet.Shapes.AddShape(ShapeGeometryPreset.OvalCallout, worksheet("J2:L7"))

Change a Shape’s Location

Use the following API to define a shape’s location in a worksheet:

MemberDescription
FloatingObject.MoveMoves the shape by the specified offsets.
FloatingObject.TopSpecifies the distance between the top edge of the worksheet and the shape’s top left corner.
FloatingObject.LeftSpecifies the distance between the left edge of the worksheet and the shape’s top left corner.
FloatingObject.TopLeftCellSpecifies the cell where the shape’s top left corner is located.
FloatingObject.BottomRightCellSpecifies the cell where the shape’s bottom right corner is located.
FloatingObject.OffsetXSpecifies the distance between the shape’s top left corner and the left edge of the TopLeftCell cell.
FloatingObject.OffsetYSpecifies the distance between the shape’s top left corner and the top edge of the TopLeftCell cell.
csharp
shape2.TopLeftCell = worksheet.Cells["F4"];
shape2.BottomRightCell = worksheet.Cells["H9"];
vb
shape2.TopLeftCell = worksheet.Cells("F4")
shape2.BottomRightCell = worksheet.Cells("H9")

Resize and Rotate a Shape

Use the following properties to rotate and resize a shape:

PropertyDescription
Shape.RotationSpecifies the shape’s rotation angle.
FloatingObject.WidthSpecifies the shape’s width.
FloatingObject.HeightSpecifies the shape’s height.
csharp
shape3.Rotation = 45;
shape3.Width = 2.2f;
shape3.Height = 1.5f;
vb
shape3.Rotation = 45
shape3.Width = 2.2F
shape3.Height = 1.5F

Change a Shape’s Form

Use the ShapeGeometry.Preset property to change a shape’s geometry type. The example below shows how to transform a rectangular callout into a cloud callout.

csharp
shape1.ShapeGeometry.Preset = ShapeGeometryPreset.CloudCallout;
vb
shape1.ShapeGeometry.Preset = ShapeGeometryPreset.CloudCallout

See Also

Charts and Graphics in Spreadsheet Documents

How to: Add Text to a Shape

How to: Change a Shape's Fill and Outline Color

How to: Create a Shape Connector

How to: Create a Shape Group