如何在记事本C# winforms中执行Find/FindNext操作

发布于 2024-11-27 05:57:28 字数 112 浏览 3 评论 0原文

请任何人建议我一些关于如何在 C# 的记事本程序中执行 find/findNext 操作的想法。我想搜索 RichTextBox 中所有出现的字符串,并在单击 findNext 按钮时突出显示每个出现的字符串。

Please anyone suggest me some ideas regarding how to perform find/findNext operation in notepad program in C#. I want to search for all occurrence of strings in RichTextBox and highlight each occurrence on click of findNext button.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与风相奔跑 2024-12-04 05:57:28

我用 C# 创建了一个记事本克隆,它实现了与 Window 记事本相同的 find / findnext 操作。您可以在这里找到源代码:

http://www.notepad-clone-in-net-winforms.html simplegoodcode.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。您看到的几个属性(例如 SelectionStartSelectionLengthContent)本质上是 TextBox 的别名SelectionStartSelectionLengthText 属性。

I created a notepad clone in C# that implements the find / findnext operation identical to Window's notepad. You can find the source here:

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

Here is what the code for the function looks like in the application:

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

This method is on the main form. It accounts for the options in the "Find" dialog box. It stores the parameter values in order to be able to perform a "Find Next"/F3 later. Several of the properties you see like SelectionStart, SelectionLength, and Content are essentially aliases to the the TextBox's SelectionStart, SelectionLength, and Text properties.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文