Live SpreadsheetGear API Samples
Shapes Form Controls Button
Add a button to a worksheet. When attached to a WorkbookView, use the WorkbookView.ShapeAction(...) event to handle clicks on a button.
// Create a new workbook and get a reference to the active worksheet and window
// info.
SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook();
SpreadsheetGear.IWorksheet worksheet = workbook.ActiveWorksheet;
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(0.5);
double top = windowInfo.RowToPoints(0.5);
double right = windowInfo.ColumnToPoints(3.5);
double bottom = windowInfo.RowToPoints(2.5);
double width = right - left;
double height = bottom - top;
// Add a button using the calculated bounds.
SpreadsheetGear.Shapes.IShape shape =
worksheet.Shapes.AddFormControl(
SpreadsheetGear.Shapes.FormControlType.ButtonControl,
left, top, width, height);
// Give the first button a name if desired.
shape.Name = "MyButton";
// Set the text of the first button.
shape.TextFrame.Characters.Text = "My Button";
Run This Sample
Download File
Download an Excel Open XML Workbook (*.xlsx) file with the results of this sample.