如何以编程方式在webBrowser控件中选择文本? C#

这是问题:我想让我的程序用户在webBrowser控件中搜索给定的关键字(标准Ctrl + F)。 我在文档中找到关键字并使用span和replace()函数突出显示所有实例都没有问题。 我无法获得我想要工作的“找到下一个”function。 当用户单击“查找下一个”时,我希望文档滚动到下一个实例。 如果我能得到一个边界框,我可以使用导航function。 我使用以下代码在富文本框中使用相同的function

//Select the found text this.richTextBox.Select(matches[currentMatch], text.Length); //Scroll to the found text this.richTextBox.ScrollToCaret(); //Focus so the highlighting shows up this.richTextBox.Focus(); 

任何人都可以提供一种方法来使其在webBrowser中工作吗?

我在具有嵌入式Web浏览器控件的WinForms应用程序中实现了搜索function。 它有一个单独的文本框,用于输入搜索字符串和“查找”按钮。 如果自上次搜索后搜索字符串已更改,则单击按钮意味着常规查找,如果不是,则表示“再次查找”。 这是按钮处理程序:

 private IHTMLTxtRange m_lastRange; private AxWebBrowser m_browser; private void OnSearch(object sender, EventArgs e) { if (Body != null) { IHTMLTxtRange range = Body.createTextRange(); if (! m_fTextIsNew) { m_lastRange.moveStart("word", 1); m_lastRange.setEndPoint("EndToEnd", range); range = m_lastRange; } if (range.findText(m_txtSearch.Text, 0, 0)) { try { range.select(); m_lastRange = range; m_fTextIsNew = false; } catch (COMException) { // don't know what to do } } } } private DispHTMLDocument Document { get { try { if (m_browser.Document != null) { return (DispHTMLDocument) m_browser.Document; } } catch (InvalidCastException) { // nothing to do } return null; } } private DispHTMLBody Body { get { if ( (Document != null) && (Document.body != null) ) { return (DispHTMLBody) Document.body; } else { return null; } } } 

m_fTextIsNew在搜索框的TextChanged处理程序中设置为true。

希望这可以帮助。

编辑:添加了正文和文档属性