テキスト検索

概要

ReoGridは、1つまたは複数のワークシートのセル全体でテキストを検索できる組み込みのテキスト検索APIを提供しています。結果を1つずつナビゲートしたり、一致するセルをハイライトしたり、すべての一致を一度に取得したりできます。

テキスト検索クラスは unvell.ReoGrid.TextSearch 名前空間にあります。

using unvell.ReoGrid.TextSearch;

基本的な使い方

単一ワークシートの検索

TextSearchSession に単一の Worksheet を渡して、そのワークシート内を検索します。

var session = new TextSearchSession(grid.CurrentWorksheet, "hello");
session.Search();

// Navigate to the next match
var match = session.NextMatch();
if (match != null)
{
    grid.CurrentWorksheet.SelectionRange = match.Cell.PositionAsRange;
}

ワークブック全体の検索

IWorkbook インスタンス(グリッドコントロール)を渡して、すべてのワークシートを検索します。

var session = new TextSearchSession(grid, "hello");
session.Search();

var match = session.NextMatch();
if (match != null)
{
    match.Cell.Worksheet.SelectionRange = match.Cell.PositionAsRange;
}

特定のワークシートの検索

ワークシートのリストを渡して、検索範囲を限定します。

var sheets = new List<Worksheet> { grid.Worksheets[0], grid.Worksheets[1] };
var session = new TextSearchSession(sheets, "hello");
session.Search();

結果のナビゲーション

Search() を呼び出した後、NextMatch()PreviousMatch() を使用して結果をナビゲートします。一致がない場合、両方のメソッドは null を返します。

// Move forward
var match = session.NextMatch();

// Move backward
var match = session.PreviousMatch();

ループ検索

デフォルトでは LoopSearchtrue で、最後の一致から最初の一致に戻る(およびその逆の)ラップアラウンドナビゲーションが行われます。終端で停止するには false に設定します。

session.LoopSearch = false;

var match = session.NextMatch();
if (match == null)
{
    MessageBox.Show("No more results.");
}

すべての一致の取得

Matches プロパティを使用して、結果の完全なリストに一度にアクセスします。

var session = new TextSearchSession(grid, "hello");
session.Search();

foreach (var match in session.Matches)
{
    Console.WriteLine($"Found at: {match.Cell.Position}, offset: {match.TextIndex}");
}

ITextSearchMatch インターフェースは以下を公開します:

プロパティ説明
CellCell一致したテキストを含むセル
TextIndexintセルの表示テキスト内での一致の文字位置

一致のハイライト

HighlightTextSearchSession を使用して、一致するすべてのセルを視覚的にハイライトします。

var session = new HighlightTextSearchSession(grid, "hello", grid.CurrentWorksheet);
session.Search();

// Highlight all matches in gold
session.MarkAllResultHighlight(SolidColor.Goldenrod);

// Navigate
var match = session.NextMatch();
if (match != null)
{
    match.Cell.Worksheet.SelectionRange = match.Cell.PositionAsRange;
}

すべてのハイライトを削除するには(例えば、検索キーワードが変更されたとき):

session.UnmarkAllResultHighlight();

完全な例:検索ダイアログ

以下の例は、次へ / 前へ / すべて検索機能を持つ簡単な検索ダイアログの構築方法を示しています。

HighlightTextSearchSession searchSession = null;

HighlightTextSearchSession GetOrCreateSession(string keyword)
{
    if (searchSession == null)
    {
        searchSession = new HighlightTextSearchSession(grid, keyword, grid.CurrentWorksheet);
        searchSession.Search();
        searchSession.MarkAllResultHighlight(SolidColor.Goldenrod);
        searchSession.LoopSearch = true;
    }
    return searchSession;
}

void OnKeywordChanged(string newKeyword)
{
    // Clear highlights from previous session
    searchSession?.UnmarkAllResultHighlight();
    searchSession = null;
}

void FindNext(string keyword)
{
    var match = GetOrCreateSession(keyword).NextMatch();
    if (match != null)
        match.Cell.Worksheet.SelectionRange = match.Cell.PositionAsRange;
}

void FindPrevious(string keyword)
{
    var match = GetOrCreateSession(keyword).PreviousMatch();
    if (match != null)
        match.Cell.Worksheet.SelectionRange = match.Cell.PositionAsRange;
}

IEnumerable<ITextSearchMatch> FindAll(string keyword)
{
    return GetOrCreateSession(keyword).Matches;
}

APIリファレンス

TextSearchSession

メンバー説明
TextSearchSession(IWorkbook, string, Worksheet?)ワークブック内のすべてのワークシートを検索
TextSearchSession(IEnumerable<Worksheet>, string, Worksheet?)指定されたワークシートを検索
TextSearchSession(Worksheet, string)単一のワークシートを検索
void Search()検索を実行。結果をナビゲートする前に呼び出す必要があります
ITextSearchMatch NextMatch()次の一致を返します。ない場合は null
ITextSearchMatch PreviousMatch()前の一致を返します。ない場合は null
IEnumerable<ITextSearchMatch> Matchesすべての一致結果
ITextSearchMatch CurrentMatch現在の一致位置を取得または設定
bool LoopSearch終端に達したときにラップアラウンドするかどうか(デフォルト:true
string Text検索キーワード
List<Worksheet> Worksheets検索対象のワークシート

HighlightTextSearchSession

TextSearchSession を拡張し、セルのハイライト機能を追加します。

メンバー説明
void MarkAllResultHighlight(SolidColor color)指定した色で一致するすべてのセルをハイライト
void UnmarkAllResultHighlight()すべてのハイライトを削除

ページの内容は役に立ちましたか?