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.

168

Focus Cell

Before 0.8.8.4, focused cell not be shown on worksheet; After 0.8.8.4, a highlight cell without background filled shows as focused cell:

195

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:

287

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:

  1. Clicked on worksheet, start to select range (SelectionChanged invoked)
  2. Dragging mouse to modify selection range (SelectionChanging invoked)
  3. 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.

164

It is possible to change the moving direction to vertical, after changing, the focus cell will move to the cell at next row:

165

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;
  }
}

Next: Border

10 Responses to “Selection”

  1. Polycarp says:

    Hey, a quick comment. How does one do the selection using a mouse? Thanks.

    • Jing says:

      Hi, did you mean select a range by programming, please try worksheet.SelectionRange = new ReoGridRange(“A1:D5”);

  2. arlodvig says:

    The moving direction by enter key can be changed. But it seems tha the tab key has the same behaviour than the enter key
    In Excel
    the enter key: the focus cell will move to the cell at next row
    Tab key: the focus cell will move to right cell

    Is it possible to have the same behaviour

    Thanks

    • Jing says:

      Currently the Tab and Enter in ReoGrid have some behavior, if you changed the move direction, both Tab and Enter will be changed.

  3. Arlodvig says:

    But is there a possibility to separate tab and enter keys?
    Is expected in a future release?

    • Jing says:

      OK, we will try to implement the different Tab and Enter keys behavior like Excel in future versions.

  4. arlodvig says:

    Thank
    I looked forward to this new version

  5. Prakash says:

    Is there a way to remove cell highlight(first focus cell) when few cells are selected?

  6. jlo says:

    hi is there a selection change event in VB.net for ReoGrid?