Focus and Selection
Selection Range vs. Focus Cell
There are two activated objects on worksheet: Selection Range and Focus Cell.
Selection Range
The selection range contains multiple cells, can be selected by user, by mouse and keyboard, as well as by programming, which is usually to be used as argument to do range operations on worksheet, such as setting style and borders, deleting content from range, or copying content into Clipboard. Below is the selection on range B2:D3
.
Focus Cell
A highlight cell without background filled shows as focused cell:
Get or set selection
To get or set the selection of worksheet, use SelectionRange
property of worksheet:
C#:
var range = worksheet.SelectionRange;
worksheet.SelectionRange = new RangePosition("A1:D5");
VB.NET:
Dim range = worksheet.SelectionRange
worksheet.SelectionRange = new RangePosition("A1:D5")
Change selection style
To change the selection style, use SelectionStyle property of worksheet:
worksheet.SelectionStyle = WorksheetSelectionStyle.FocusRect;
The following styles are supported:
Change selection mode
It is possible to change selection mode for worksheet:
worksheet.SelectionMode = WorksheetSelectionMode.Row;
The following mode are supported:
Enum Value | Description |
---|---|
None | Do not allow to select anything on worksheet. |
Cell | Only allow to select single cell. |
Range | Allow to select cell or ranges. |
Row | Always to select one or more entire rows at a time. |
Column | Always to select one or more entire columns at a time. |
Detecting selection changes
To detect selection changes, use SelectionChanged
or SelectionChanging
events:
C#
worksheet.SelectionChanged += (s, e) =>
MessageBox.Show(worksheet.SelectionRange.ToAddress());
VB.NET
AddHandler worksheet.SelectionRangeChanged, Sub(s, e) _
MessageBox.Show(e.Range.ToAddress())
SelectionChanged
invoked after the selection changed, if end-user drags mouse on worksheet during selection operation, only the SelectionChanging
will be invoked. Work flow is below:
- Clicked on worksheet, start to select range (
SelectionChanged
invoked) - Dragging mouse to modify selection range (
SelectionChanging
invoked) - Release mouse, finish the select operation (
SelectionChanged
invoked)
Both these two events will only be invoked once when the selection changed actually.
Native behaviors
ReoGrid makes focus cell always be included in selection range. When the selection range changed, focus cell will also be changed, instead when focus cell changed, the selection range will also be changed, in this case, the focus cell equals selection range.
Change focus cell position
To change the focus cell position, use the FocusPos
property of worksheet.
C#:
var focusPos = sheet.FocusPos; // get
sheet.FocusPos = new CellPosition("D5"); // set
VB.NET:
Dim focusPos = sheet.FocusPos ' get
sheet.FocusPos = new CellPosition("D5") ' set
Change focus cell move direction
By default, when press Enter key on worksheet the focus cell moved to right cell on worksheet, this is horizontal moving direction.
It is possible to change the moving direction to vertical, after changing, the focus cell will move to the cell at next row:
To change focus cell move direction from horizontal to vertical:
worksheet.SelectionStyle = WorksheetSelectionStyle.FocusRect;
BeforeSelectionRangeChange Event
The BeforeSelectionRangeChange
event will be fired when end-user attempts to change the selection, but built-in operations has not be performed. By setting the property IsCancelled
of this event argument can make the built-in operations won't be performed.
This event also has the properties that allow user-code to change the selection instead of end-user selected range. The example below shows how to limit the number of rows and columns for a selection operation.
Limit selection range maximum rows and columns
Add a handler to event BeforeSelectionRangeChange
:
worksheet.BeforeSelectionRangeChange += worksheet_BeforeSelectionRangeChange;
By changing the selection end position to limit the maximum number of rows to 5:
void worksheet_BeforeSelectionRangeChange(object sender, BeforeSelectionChangeEventArgs e)
{
int rows = Math.Abs(e.StartRow - e.EndRow);
if (rows >= 5)
{
if (e.EndRow > e.StartRow)
e.EndRow = e.StartRow + 5 - 1;
else
e.EndRow = e.StartRow - 5 + 1;
}
}