2

This is a SpreadsheetGear Grid specific question. I know you can add a comment to a cell and the cell gets red triangle marker at the top right corner automatically. But I have a need to add a small triangle (different color) to any cell corner to indicate something special. Is it possible to do it?

UPATE: This is what I got for adding a triangle to any corner of the cell based on Daniel's suggestion.

    public void AddTriangleShapeToCorner(IWorksheet worksheet, int row, int col, CellCorners cellCorner)
    {
        const double width = 5, height = 5;
        double xOffset = 0, yOffset = 0;

        IWorksheetWindowInfo windowInfo = worksheet.WindowInfo;

        if (cellCorner == CellCorners.TopRight || cellCorner == CellCorners.BottomRight)
        {
            col++;
            xOffset = width;
        }
        if (cellCorner == CellCorners.BottomLeft || cellCorner == CellCorners.BottomRight)
        {
            row++;
            yOffset = height;
        }
        double top = windowInfo.RowToPoints(row) - yOffset;
        double left = windowInfo.ColumnToPoints(col) - xOffset;

        IShape shape = worksheet.Shapes.AddShape(AutoShapeType.RightTriangle, left, top, width, height);
        shape.Line.Visible = false;         // line at top-left corner is not sharp, so turn it off.
        shape.Placement = Placement.Move;   // make the shape move with cell. NOTE: it doesn't work for top-right and bottom-right corners.
        if (cellCorner == CellCorners.TopLeft || cellCorner == CellCorners.TopRight)
            shape.VerticalFlip = true;
        if (cellCorner == CellCorners.TopRight || cellCorner == CellCorners.BottomRight)
            shape.HorizontalFlip = true;
    }
newman
  • 6,841
  • 21
  • 79
  • 126

1 Answers1

1

You can use the IShapes AddShape method. For the type, you can use AutoShapeType.RightTriangle.

Here is an example:

private void AddTriangleShape(SpreadsheetGear.IWorksheet worksheet, int iRow, int iCol)
{
  SpreadsheetGear.IWorksheetWindowInfo windowInfo = worksheet.WindowInfo;

  // Calculate the left, top, width and height of the button by 
  // converting row and column coordinates to points.  Use fractional 
  // values to get coordinates in between row and column boundaries.
  double left = windowInfo.ColumnToPoints(iCol);
  double top = windowInfo.RowToPoints(iRow + 0.5);
  double right = windowInfo.ColumnToPoints(iCol + 0.1);
  double bottom = windowInfo.RowToPoints(iRow + 0.9);
  double width = right - left;
  double height = bottom - top;

  worksheet.Shapes.AddShape(SpreadsheetGear.Shapes.AutoShapeType.RightTriangle, left, top, width, height);
}
Daniel
  • 5,602
  • 4
  • 33
  • 36
  • Thank you very much for this workaround. It works, but has two issues: 1. The triangle shape can be selected, moved or deleted. I would like to disable all those actions. There is a Locked property on the shape, but it works only if the sheet is locked which is not what I want. 2. The shape is anchored to the cell it sits in. If you set the triangle to the top right corner, and you resize the column to its right, the shape will not move. Do you have any idea how to resolve those issues? – newman Dec 10 '12 at 17:58
  • 1
    Nice work on the AddTriangleShapeToCorner method! I don't have any great solutions for your two issues, but I have a couple ideas that may help you think of solutions. For issue 1, the WorkbookView class has a event called ShapeSelectionChanging where you can cancel the selection (e.Cancel = true;). That will prevent the shape from being selected or deleted, but unfortunately it doesn't stop the shape from being moved. – Daniel Dec 10 '12 at 23:41
  • For issue 2, the WorkbookView class also has an event called RangeChanged where you can get the column width (e.Range.ColumnWidth). Using that you could try to move the RightTriangle objects in that column. – Daniel Dec 10 '12 at 23:42
  • 1
    For issue 2, I got similar idea as yours and it's working now. For issue 1, your tip of ShapeSelectionChanging event does help. The only issue remaining is, as you pointed out, that user can still move the shape. Unfortunately, Shape object doesn't expose any events. Thank you very much for your great help. – newman Dec 11 '12 at 19:23