C# 如何编写查找函数

发布于 2024-10-03 10:35:16 字数 1104 浏览 0 评论 0原文

我有一个使用查找函数的 C# 程序,但是它能够找到该单词,但不会突出显示 richTextBox 中找到的单词。

有人可以建议我有关代码吗?

谢谢。

查找函数类形式:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Syscrawl
{
public partial class Find_Form : Form
{
    FTK_Menu_Browsing_History fmbh = new FTK_Menu_Browsing_History();

    public Find_Form()
    {
        InitializeComponent();
    }

    public void searchButton_Click(object sender, EventArgs e)
    {
        string s1 = fmbh.getSearchBrowsing().ToLower();
        string s2 = textBoxSearch.Text.ToLower();

        if (s1.Contains(s2))
        {
            MessageBox.Show("Word found!");

            this.fmbh.richTextBoxBrowsing.Find(s2);
            this.fmbh.richTextBoxBrowsing.SelectionLength = s2.Length;
            this.fmbh.richTextBoxBrowsing.SelectionColor = Color.Red;
            this.Close();
        }
        else
        {
            MessageBox.Show("Word not found!");
        }
    }
}
}

I have a C# program that utilizes a find function however it is able to find the word but does not highlights the found word in the richTextBox.

Can someone please advise me on the codes?

Thanks.

Find Function Class Form:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Syscrawl
{
public partial class Find_Form : Form
{
    FTK_Menu_Browsing_History fmbh = new FTK_Menu_Browsing_History();

    public Find_Form()
    {
        InitializeComponent();
    }

    public void searchButton_Click(object sender, EventArgs e)
    {
        string s1 = fmbh.getSearchBrowsing().ToLower();
        string s2 = textBoxSearch.Text.ToLower();

        if (s1.Contains(s2))
        {
            MessageBox.Show("Word found!");

            this.fmbh.richTextBoxBrowsing.Find(s2);
            this.fmbh.richTextBoxBrowsing.SelectionLength = s2.Length;
            this.fmbh.richTextBoxBrowsing.SelectionColor = Color.Red;
            this.Close();
        }
        else
        {
            MessageBox.Show("Word not found!");
        }
    }
}
}

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

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

发布评论

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

评论(2

墨洒年华 2024-10-10 10:35:16

您需要首先选择您要查找的内容。这:

int offset = s1.IndexOf(s2);
richTextBox1.Select(offset, s2.Length);

之后你就可以进行整个突出显示。另一个提示,为了防止选择过程中闪烁,请在表单中使用以下代码:

protected override void WndProc(ref Message m)
{
    if (m.Msg == 0) {
        if (!_doPaint)
            return;
    }

    base.WndProc(ref m);
} 

在选择任何内容之前将 _doPaint 设置为 false,在选择之后将其设置为 true。

希望我能帮忙!

You need to select what you are looking for first. This:

int offset = s1.IndexOf(s2);
richTextBox1.Select(offset, s2.Length);

After that you can make the whole highlightining. Another tip, to prevent the flickering in the selection process, use this code in your form:

protected override void WndProc(ref Message m)
{
    if (m.Msg == 0) {
        if (!_doPaint)
            return;
    }

    base.WndProc(ref m);
} 

Before selecting anything set _doPaint to false and after the selection set it to true.

Hope I can help!

っ〆星空下的拥抱 2024-10-10 10:35:16

您需要调用 s1.IndexOf(s2, StringComparison.CurrentCultureIgnoreCase) 来查找匹配的位置。

此外,看起来您的“查找”表单创建了自己的“历史记录”表单实例;它不使用现有实例。
您应该考虑接受构造函数参数。

You need to call s1.IndexOf(s2, StringComparison.CurrentCultureIgnoreCase) to find the position of the match.

Also, it looks like your Find form creates its own instance of the History form; it doesn't use the existing instance.
You should consider accepting a constructor parameter.

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