0

C#のメモ帳プログラムでfind/findNext操作を実行する方法に関するアイデアを誰かに提案してください。RichTextBoxですべての文字列を検索し、findNextボタンをクリックして各文字列を強調表示したいと思います。

4

2 に答える 2

1

次のコードをご覧ください: http://www.dreamincode.net/code/snippet2466.htmおよびハイライトについては、C# を使用して TextBox/Label/RichTextBox 内のテキストをハイライトします。

于 2011-07-30T11:39:24.577 に答える
0

Windows のメモ帳と同じ find / findnext 操作を実装するメモ帳のクローンを C# で作成しました。ここでソースを見つけることができます:

http://www.simplygoodcode.com/2012/04/notepad-clone-in-net-winforms.html

アプリケーションでの関数のコードは次のようになります。

    private string _LastSearchText;
    private bool _LastMatchCase;
    private bool _LastSearchDown;

    public bool FindAndSelect(string pSearchText, bool pMatchCase, bool pSearchDown) {
        int Index;

        var eStringComparison = pMatchCase ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase;

        if (pSearchDown) {
            Index = Content.IndexOf(pSearchText, SelectionEnd, eStringComparison);
        } else {
            Index = Content.LastIndexOf(pSearchText, SelectionStart, SelectionStart, eStringComparison);
        }

        if (Index == -1) return false;

        _LastSearchText = pSearchText;
        _LastMatchCase = pMatchCase;
        _LastSearchDown = pSearchDown;

        SelectionStart = Index;
        SelectionLength = pSearchText.Length;

        return true;
    }

このメソッドはメイン フォームにあります。[検索] ダイアログ ボックスのオプションを説明します。後で「次を検索」/F3 を実行できるように、パラメータ値を保存します。SelectionStartSelectionLength、およびのように表示されるプロパティのいくつかは、基本的にの、、およびプロパティのContentエイリアスです。TextBoxSelectionStartSelectionLengthText

于 2012-08-01T19:15:58.043 に答える