ReoGridは、セル、範囲、行、列の選択を管理するための包括的な選択システムを提供しており、複数の非連続選択、カスタム選択動作、豊富なイベント処理をサポートしています。
選択範囲とフォーカスセル
ワークシートには2つのアクティブなオブジェクトがあります:選択範囲とフォーカスセルです。
選択範囲は複数のセルを含み、マウスやキーボードによるユーザー操作、またはプログラムによって選択できます。通常、スタイルや罫線の設定、内容の削除、クリップボードへのコピーなど、ワークシート上の範囲操作の引数として使用されます。以下は範囲 B2:D3 の選択です。

塗りつぶし背景のないハイライトされたセルがフォーカスセルとして表示されます:

ReoGridは常にフォーカスセルを選択範囲内に保持します。選択範囲が変更されると、フォーカスセルも変更されます。逆に、フォーカスセルが変更されると選択範囲も変更されます。この場合、選択範囲はフォーカスセルと同じになります。
選択範囲
現在の選択範囲の取得
// Get the current selection range
RangePosition selection = worksheet.SelectionRange;
// Access individual properties
int startRow = selection.Row;
int startCol = selection.Col;
int rows = selection.Rows;
int cols = selection.Cols;
int endRow = selection.EndRow;
int endCol = selection.EndCol;
プログラムによる選択の設定
// Select by address string
worksheet.SelectRange("B2:D5");
// Select by row, col, rows, cols
worksheet.SelectRange(1, 1, 4, 3);
// Select by RangePosition
worksheet.SelectRange(new RangePosition(1, 1, 4, 3));
// Select by two cell positions (start and end)
worksheet.SelectRange(new CellPosition(1, 1), new CellPosition(4, 3));
// Select entire rows
worksheet.SelectRows(2, 3); // 3 rows starting from row 2
// Select entire columns
worksheet.SelectColumns(1, 2); // 2 columns starting from column 1
// Select all cells
worksheet.SelectAll();
SelectRange(range, scrollToSelectRange) の2番目のパラメータは、選択範囲を表示するためにビューをスクロールするかどうかを制御します(デフォルト:true):
// Select without scrolling
worksheet.SelectRange(new RangePosition(100, 0, 1, 1), scrollToSelectRange: false);
フォーカスセル
フォーカスセルは、キーボード入力が送られる選択範囲内の単一のアクティブセルです:
// Get the current focus position
CellPosition focusPos = worksheet.FocusPos;
// Set the focus position
worksheet.FocusPos = new CellPosition("C3");
worksheet.FocusPos = new CellPosition(2, 2);
フォーカス位置のスタイル
フォーカスセルの表示方法を制御します:
// Default style (highlighted cell)
worksheet.FocusPosStyle = FocusPosStyle.Default;
// No special display on focus cell
worksheet.FocusPosStyle = FocusPosStyle.None;
ホバー位置
マウスカーソル下のセルを取得します:
CellPosition hoverPos = worksheet.HoverPos;
選択モード
ユーザーが選択できる対象を制御します:
worksheet.SelectionMode = WorksheetSelectionMode.Range;
| モード | 説明 |
|---|---|
None | 選択が無効 |
Cell | 単一セルのみ選択可能 |
Range | セルまたは範囲を選択可能(デフォルト) |
Row | 常に行全体を選択 |
Column | 常に列全体を選択 |
選択スタイル
選択の外観を制御します:
デフォルトスタイル
worksheet.SelectionStyle = WorksheetSelectionStyle.Default;

Windowsフォーカス矩形スタイル
worksheet.SelectionStyle = WorksheetSelectionStyle.FocusRect;

選択の非表示
worksheet.SelectionStyle = WorksheetSelectionStyle.None;

選択の移動方向
編集終了後(Enterキー)にフォーカスが移動する方向を制御します:
// Move right (next cell in the same row) — default
worksheet.SelectionForwardDirection = SelectionForwardDirection.Right;

// Move down (cell in the next row)
worksheet.SelectionForwardDirection = SelectionForwardDirection.Down;

// Auto — Enter moves down, Tab moves right
worksheet.SelectionForwardDirection = SelectionForwardDirection.Auto;
複数選択
ReoGridは AllowMultipleSelection が有効な場合、非連続選択をサポートしています(Ctrl/Cmdキーを押しながらクリック):
// Enable multiple selection
worksheet.AllowMultipleSelection = true;
// Get the primary selection
var primary = worksheet.SelectionRange;
// Get all selected blocks
foreach (var range in worksheet.SelectionRanges)
{
Console.WriteLine(range.ToAddress());
}
SelectionRangesコレクション
| プロパティ/メソッド | 説明 |
|---|---|
Count | 選択された範囲の数 |
Add(RangePosition) | 選択に範囲を追加 |
Remove(RangePosition) | 選択から範囲を削除 |
Clear() | すべての選択をクリア |
Contains(RangePosition) | 範囲が選択されているか確認 |
注意: 組み込みの編集コマンド(削除、貼り付け、入力)はアクティブな選択(最後にクリックした範囲)に対して動作します。すべての選択に操作を適用するには、
SelectionRangesを反復処理してください:foreach (var range in worksheet.SelectionRanges) { worksheet.SetRangeStyles(range, style); }
Ctrlキーによる複数行選択
// Allow selecting multiple rows with Ctrl+click
worksheet.AllowMultipleRowWithCtrlKey = true;
選択の移動
プログラムによる移動
// Move in a direction
worksheet.MoveSelection(SelectionMoveDirection.Right);
worksheet.MoveSelection(SelectionMoveDirection.Down);
// Directional moves with optional extend (Shift-select behavior)
worksheet.MoveSelectionUp(appendSelect: false);
worksheet.MoveSelectionDown(appendSelect: false);
worksheet.MoveSelectionLeft(appendSelect: false);
worksheet.MoveSelectionRight(appendSelect: false);
// Move to beginning/end of row or column
worksheet.MoveSelectionHome(RowOrColumn.Column); // Move to first row
worksheet.MoveSelectionHome(RowOrColumn.Row); // Move to first column
worksheet.MoveSelectionEnd(RowOrColumn.Column); // Move to last row
worksheet.MoveSelectionEnd(RowOrColumn.Row); // Move to last column
// Page up/down
worksheet.MoveSelectionPageUp();
worksheet.MoveSelectionPageDown();
// Move focus forward (Enter behavior)
worksheet.MoveSelectionForward();
worksheet.MoveSelectionBackward();
// Move focus right
worksheet.MoveFocusRight(autoReturn: true); // Wrap to next row at end
// Move focus down
worksheet.MoveFocusDown(autoReturn: true); // Wrap to next column at end
ロックされていないセルへの移動
ワークシートがロックされている場合、次のロックされていないセルに選択を移動します:
worksheet.MoveSelectionRangeToUnlockedCell();
カーソルスタイル
ワークシート上のカーソルスタイルを変更します:
worksheet.ChangeCursor(CursorStyle.Hand);
イベント
選択変更イベント
// Before selection changes (can be cancelled)
worksheet.BeforeSelectionRangeChange += (s, e) =>
{
// Access the proposed selection
Console.WriteLine($"Selection changing to: ({e.StartRow},{e.StartCol})-({e.EndRow},{e.EndCol})");
// Cancel the change
e.IsCancelled = true;
// Or modify the proposed range
e.EndRow = Math.Min(e.EndRow, e.StartRow + 4); // Limit to 5 rows
};
// During selection change (while dragging)
worksheet.SelectionRangeChanging += (s, e) =>
{
// e.Range contains the current selection during drag
};
// After selection is finalized
worksheet.SelectionRangeChanged += (s, e) =>
{
Console.WriteLine("Selection changed: " + e.Range.ToAddress());
};
注意:
SelectionRangeChangedはマウスボタンが離されたときにのみ発生し、ドラッグ中には発生しません。マウスドラッグ中のリアルタイムの選択変更を追跡するにはSelectionRangeChangingを使用してください。
選択移動イベント
// Forward movement (Enter key)
worksheet.SelectionMovedForward += (s, e) =>
{
e.IsCancelled = true; // Prevent default behavior
// Custom: always move to first cell of next row
worksheet.SelectionRange = new RangePosition(
worksheet.SelectionRange.Row + 1, 0, 1, 1);
};
// Backward movement (Shift+Enter)
worksheet.SelectionMovedBackward += (s, e) =>
{
e.IsCancelled = true;
// Custom backward logic
};
その他の選択イベント
// Selection mode changed
worksheet.SelectionModeChanged += (s, e) => { };
// Selection style changed
worksheet.SelectionStyleChanged += (s, e) => { };
// Selection forward direction changed
worksheet.SelectionForwardDirectionChanged += (s, e) => { };
// Focus position changed
worksheet.FocusPosChanged += (s, e) => { };
// Focus position style changed
worksheet.FocusPosStyleChanged += (s, e) => { };
// Hover position changed (mouse moved over a different cell)
worksheet.HoverPosChanged += (s, e) => { };
BeforeSelectionChangeEventArgsのプロパティ
| プロパティ | 型 | 説明 |
|---|---|---|
SelectionStart | CellPosition | 提案された選択の開始位置 |
SelectionEnd | CellPosition | 提案された選択の終了位置 |
StartRow | int | 開始行(変更可能) |
StartCol | int | 開始列(変更可能) |
EndRow | int | 終了行(変更可能) |
EndCol | int | 終了列(変更可能) |
IsCancelled | bool | true に設定すると選択変更をキャンセル |
選択範囲の制限
最大行数と列数の制限
worksheet.BeforeSelectionRangeChange += (s, e) =>
{
int maxRows = 5;
int maxCols = 3;
int rows = Math.Abs(e.StartRow - e.EndRow);
if (rows >= maxRows)
{
e.EndRow = e.EndRow > e.StartRow
? e.StartRow + maxRows - 1
: e.StartRow - maxRows + 1;
}
int cols = Math.Abs(e.StartCol - e.EndCol);
if (cols >= maxCols)
{
e.EndCol = e.EndCol > e.StartCol
? e.StartCol + maxCols - 1
: e.StartCol - maxCols + 1;
}
};
選択を特定の領域に制限
worksheet.BeforeSelectionRangeChange += (s, e) =>
{
// Only allow selection in B2:F10
if (e.StartRow < 1 || e.StartCol < 1 || e.EndRow > 9 || e.EndCol > 5)
{
e.IsCancelled = true;
}
};
キーボードイベントによるカスタムナビゲーション
キーボードイベントを使用してナビゲーション動作をオーバーライドします:
worksheet.BeforeCellKeyDown += (s, e) =>
{
if (e.KeyCode == Keys.Enter)
{
e.IsCancelled = true;
// Always move to first cell of next row
worksheet.SelectionRange =
new RangePosition(worksheet.SelectionRange.Row + 1, 0, 1, 1);
}
};
組み込みキーボード操作
サポートされているすべてのキーボードショートカットについては、ホットキーリファレンスを参照してください。