# Working with Selections

Learn how to manage selections programmatically in a spreadsheet, including adding, removing, and retrieving selections using code.

## Content

When a user selects a range of cells, that range of cells can have a separate background color and foreground color to distinguish it from the other cells in the spreadsheet. The range is called a selection. There are many aspects of selections that you can manage programmatically. In code you can add and remove selections and you can find out what is selected. This topic summarizes some of the tasks you can perform with selections in code.

* To add a selection (a range of cells that are displayed as selected), use the **Sheets** [AddSelection](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.SheetView.AddSelection.html) method and specify the starting row and column, and the number of rows and columns in the selection.
* To get all the ranges of cells that are presently selected, use the Sheets [GetSelections](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.SheetView.GetSelections.html) method. To return a specific selection, use the **Sheets** [GetSelection](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.SheetView.GetSelection.html) method.
* To remove all of the selections, use the **Sheets** [ClearSelection](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.SheetView.ClearSelection.html) method. To remove a specific selection, use the **Sheets** [RemoveSelection](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.SheetView.RemoveSelection.html) method and specify the row and column, and the number of rows and columns to remove from the selection.
* To clear all selections when a new active cell is set programmatically, using the Boolean clearSelection parameter in the [SetActiveCell](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.SheetView.SetActiveCell.html) method.
* To keep a selection highlighted, use the [RetainSelectionBlock](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.FpSpread.RetainSelectionBlock.html) property of the [FpSpread](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.FpSpread.html) class.
* You can move a selected cell in the view using the [MoveActiveOnFocus](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.FpSpread.MoveActiveOnFocus.html) property of the [FpSpread](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.FpSpread.html) class.
* To work with events regarding selections, refer to the [SelectionChangedEventArgs](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.SelectionChangedEventArgs.html) class.

To select all the cells in a sheet use the [RowCount](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.SheetView.RowCount.html) and [ColumnCount](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.SheetView.ColumnCount.html) properties for that sheet, as in this line of code:
`fpSpread1.ActiveSheet.Models.Selection.SetSelection(0, 0, fpSpread1.ActiveSheet.RowCount, fpSpread1.ActiveSheet.ColumnCount)`
The **DefaultSheetSelectionModel** class (and **IDisjointSelection** interface) **GetSelections** method returns -1 for either the RowCount or the ColumnCount if all the cells in that row or column are selected, as when the end user clicks on a header to make a selection.
For information on the underlying model for selections, refer to [Understanding the Selection Model](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-usemodels/spwin-model-types/spwin-model-selectstuff).
**Using a Shortcut**
To add a selection, use the **SheetView's** [AddSelection](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.SheetView.AddSelection.html) method from the **Sheets** shortcut, specifying the necessary parameters.
**Example**
This example code selects two ranges of cells.

```csharp
// Set the sheet to allow multiple range selections.
fpSpread1.Sheets[0].SelectionPolicy = FarPoint.Win.Spread.Model.SelectionPolicy.MultiRange;
// Select cells C3 through D4.
fpSpread1.Sheets[0].AddSelection(2, 2, 2, 2);
// Select cells F6 through H8.
fpSpread1.Sheets[0].AddSelection(5, 5, 3, 3);


// Select cells F6 through H8.
fpSpread1.Sheets[0].AddSelection(5, 5, 3, 3);
```

```vbnet
' Set the sheet to allow multiple range selections.
FpSpread1.Sheets(0).SelectionPolicy = FarPoint.Win.Spread.Model.SelectionPolicy.MultiRange
' Select cells C3 through D4.
FpSpread1.Sheets(0).AddSelection(2, 2, 2, 2)
' Select cells F6 through H8.
FpSpread1.Sheets(0).AddSelection(5, 5, 3, 3)
```

**Using Code**
To add a selection, use the [AddSelection](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.SheetView.AddSelection.html) method from the **Sheets** shortcut, specifying the necessary parameters. Then assign the [SheetView](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.SheetView.html) object to a sheet in the component.
**Example**
This example code selects two ranges of cells.

```csharp
FarPoint.Win.Spread.SheetView newsheet=new FarPoint.Win.Spread.SheetView();
// Add two selections.
newsheet.SelectionPolicy = FarPoint.Win.Spread.Model.SelectionPolicy.MultiRange;
newsheet.AddSelection(2, 2, 2, 2);
newsheet.AddSelection(5, 5, 3, 3);
// Assign the SheetView to a sheet in the component.
fpSpread1.Sheets[0] = newsheet;
```

```vbnet
Dim newsheet As New FarPoint.Win.Spread.SheetView()
' Add two selections.
newsheet.SelectionPolicy = FarPoint.Win.Spread.Model.SelectionPolicy.MultiRange
newsheet.AddSelection(2, 2, 2, 2)
newsheet.AddSelection(5, 5, 3, 3)
' Assign the SheetView to a sheet in the component.
FpSpread1.Sheets(0) = newsheet
```

## Working with multi-range selections

Users can format multiple cells simultaneously using the Format Cells dialog at runtime after selecting a range of cells. The **BuiltInDialogs** class provides **FormatCells** method that allows you to customize the style settings.
![](https://cdn.mescius.io/document-site-files/images/2238e618-a1fb-45a6-9ce5-9a4157bd2931/images/format-dialog-in-multirange.gif)
**Using Code**
This example code allows you to select multi-range cells, and call the Format Cells dialog to apply the style effects.

```csharp
FarPoint.Win.Spread.Dialogs.BuiltInDialogs.FormatCells(ActiveSheet.Selection).ShowDialog();
```

```vbnet
FarPoint.Win.Spread.Dialogs.BuiltInDialogs.FormatCells(ActiveSheet.Selection).ShowDialog()
```

## See Also

[Specifying What the User Can Select](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-interactcell/spwin-interactselection/spwin-selsetuser)
[Customizing the Selection Appearance](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-interactcell/spwin-interactselection/spwin-selsetappear)
[Hiding the Selection When Focus is Lost](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-interactcell/spwin-interactselection/spwin-selhidenofocus)
[Working with Deselections](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-interactcell/spwin-interactselection/working-with-deselections)