Live SpreadsheetGear API Samples
Shapes Form Controls ListBox
Add a ListBox to a worksheet, fill it with a range of values, and link it to a cell.
// Create a new workbook and get a reference to the active worksheet, cells and
// window info.
SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook();
SpreadsheetGear.IWorksheet worksheet = workbook.ActiveWorksheet;
SpreadsheetGear.IRange cells = worksheet.Cells;
SpreadsheetGear.IWorksheetWindowInfo windowInfo = worksheet.WindowInfo;
// Populate some cells which will be linked to from the ListBox.
cells["A50"].Value = "January";
cells["A51"].Value = "February";
cells["A52"].Value = "March";
cells["A53"].Value = "April";
cells["A54"].Value = "May";
cells["A55"].Value = "June";
cells["A56"].Value = "July";
cells["A57"].Value = "August";
cells["A58"].Value = "September";
cells["A59"].Value = "October";
cells["A60"].Value = "November";
cells["A61"].Value = "December";
// Calculate the left, top, width and height of the ListBox by
// converting row and column coordinates to points. Use fractional
// values to get coordinates in between row and column boundaries.
double left = windowInfo.ColumnToPoints(1.1);
double top = windowInfo.RowToPoints(1.1);
double right = windowInfo.ColumnToPoints(2.9);
double bottom = windowInfo.RowToPoints(8.9);
double width = right - left;
double height = bottom - top;
// Add the ListBox using the calculated bounds.
SpreadsheetGear.Shapes.IShape shape =
worksheet.Shapes.AddFormControl(
SpreadsheetGear.Shapes.FormControlType.ListBox,
left, top, width, height);
// Get a reference to the control format.
SpreadsheetGear.Shapes.IControlFormat controlFormat = shape.ControlFormat;
// Fill the ListBox with a range of existing values.
controlFormat.ListFillRange = "A50:A61";
// Link the ListBox to cell A2.
controlFormat.LinkedCell = "A2";
// Select the first item in the list.
controlFormat.ListIndex = 0;
Run This Sample
Download File
Download an Excel Open XML Workbook (*.xlsx) file with the results of this sample.