实施“查找下一个”在 C# 的 Web 浏览器控件中

发布于 2025-01-07 00:50:38 字数 1197 浏览 0 评论 0原文

我有一个嵌入在表单中的网络浏览器控件。表单加载后,它会加载本地存储的 HTML 文件。我已经实现了查找文本功能,以在 Web 浏览器 控件中加载的 HTML 文档中查找特定文本。它正在努力查找指定单词的第一次出现。
但我想一次性突出显示指定单词的所有出现情况,或者更好地实现类似于各种应用程序中的“查找下一个”功能的功能。是否可以对网络浏览器控制执行此操作???
这是当前的代码:

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            string TextToFind;
            TextToFind = toolStripTextBox1.Text;

            if (webBrowser1.Document != null)
            {
                IHTMLDocument2 doc = webBrowser1.Document.DomDocument as IHTMLDocument2;
                if (doc != null)
                {
                    IHTMLSelectionObject currentSelection = doc.selection;

                    IHTMLTxtRange range = currentSelection.createRange() as IHTMLTxtRange;
                    if (range != null)
                    {
                        String search = TextToFind.ToString();
                        if (range.findText(search, search.Length, 2))
                        {
                            range.select();
                        }                            
                    }
                }
            }
        }

谢谢。

I have a web browser control embedded in a form. Upon form load it loads a locally stored HTML file. I have implemented a find text functionality to find a particular text in the HTML document loaded in the web browser control. It is working for finding the first occurrence of the word specified.
But I want to highlight all the occurrences of the specified word all at once or still better implementing something analogous to "Find Next" function found in various applications. Is it possible to do that for web browser control???
Here's the current code:

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            string TextToFind;
            TextToFind = toolStripTextBox1.Text;

            if (webBrowser1.Document != null)
            {
                IHTMLDocument2 doc = webBrowser1.Document.DomDocument as IHTMLDocument2;
                if (doc != null)
                {
                    IHTMLSelectionObject currentSelection = doc.selection;

                    IHTMLTxtRange range = currentSelection.createRange() as IHTMLTxtRange;
                    if (range != null)
                    {
                        String search = TextToFind.ToString();
                        if (range.findText(search, search.Length, 2))
                        {
                            range.select();
                        }                            
                    }
                }
            }
        }

Thanks.

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

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

发布评论

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

评论(2

厌倦 2025-01-14 00:50:38

您可以在此处找到问题的代码示例 MSDN 论坛:WebBrowser 查找对话框

希望这正是您正在寻找的内容。

You will find the the code sample for your question here MSDN Forums: WebBrowser Find Dialog

Hope that is exactly what you are looking for.

踏雪无痕 2025-01-14 00:50:38

只需在文本框中获取用户的输入[此处为txtNoteSearch],然后按照以下代码即可实现搜索。以下代码演示了搜索和突出显示。

    private void WebBrowser_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
{
    mshtml.IHTMLDocument2 doc2 = WebBrowser.Document.DomDocument;
    string ReplacementTag = "<span style='background-color: rgb(255, 255, 0);'>";
    StringBuilder strBuilder = new StringBuilder(doc2.body.outerHTML);
    string HTMLString = strBuilder.ToString();
    if (this.m_NoteType == ExtractionNoteType.SearchResult)
    {
        List<string> SearchWords = new List<string>();
        SearchWords.AddRange(this.txtNoteSearch.Text.Trim.Split(" "));
        foreach (string item in SearchWords)
        {
            int index = HTMLString.IndexOf(item, 0, StringComparison.InvariantCultureIgnoreCase);
            // 'If index > 0 Then
            while ((index > 0 && index < HTMLString.Length))
            {
                HTMLString = HTMLString.Insert(index, ReplacementTag);
                HTMLString = HTMLString.Insert(index + item.Length + ReplacementTag.Length, "</span>");
                index = HTMLString.IndexOf(item, index + item.Length + ReplacementTag.Length + 7, StringComparison.InvariantCultureIgnoreCase);
            }
        }
    }
    else
    {
    }
    doc2.body.innerHTML = HTMLString;
}

Just take input from the user in a textbox [here txtNoteSearch] and then follow the following code to implement the search. Following code demonstrates the search and highlight.

    private void WebBrowser_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
{
    mshtml.IHTMLDocument2 doc2 = WebBrowser.Document.DomDocument;
    string ReplacementTag = "<span style='background-color: rgb(255, 255, 0);'>";
    StringBuilder strBuilder = new StringBuilder(doc2.body.outerHTML);
    string HTMLString = strBuilder.ToString();
    if (this.m_NoteType == ExtractionNoteType.SearchResult)
    {
        List<string> SearchWords = new List<string>();
        SearchWords.AddRange(this.txtNoteSearch.Text.Trim.Split(" "));
        foreach (string item in SearchWords)
        {
            int index = HTMLString.IndexOf(item, 0, StringComparison.InvariantCultureIgnoreCase);
            // 'If index > 0 Then
            while ((index > 0 && index < HTMLString.Length))
            {
                HTMLString = HTMLString.Insert(index, ReplacementTag);
                HTMLString = HTMLString.Insert(index + item.Length + ReplacementTag.Length, "</span>");
                index = HTMLString.IndexOf(item, index + item.Length + ReplacementTag.Length + 7, StringComparison.InvariantCultureIgnoreCase);
            }
        }
    }
    else
    {
    }
    doc2.body.innerHTML = HTMLString;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文