简体   繁体   English

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

[英]How Do I programatically select text in a webBrowser Control? c#

Here's the Problem: I want to enable the user of my program to search a webBrowser control for a given keyword (standard Ctrl+ F). 这是问题:我想让我的程序用户在webBrowser控件中搜索给定的关键字(标准Ctrl + F)。 I am having no problem finding the keyword in the document and highlighting all the instances using a span and the replace() function. 我在文档中找到关键字并使用span和replace()函数突出显示所有实例都没有问题。 I am having trouble getting the "find next" functionlity that I want to work. 有麻烦了“查找下一个” functionlity,我要工作。 When the user clicks Find next I want the document to scroll to the next instance. 当用户单击“查找下一个”时,我希望文档滚动到下一个实例。 If I could get a bounding box I can use the navigate function. 如果我能得到一个边界框,我可以使用导航功能。 I have this same functionality working in a rich text box using the following code 我使用以下代码在富文本框中使用相同的功能

                //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();

Can anyone provide a methodology to get this to work in a webBrowser? 任何人都可以提供一种方法来使其在webBrowser中工作吗?

I implemented a search feature in a WinForms app that had an embedded Web browser control. 我在具有嵌入式Web浏览器控件的WinForms应用程序中实现了搜索功能。 It had a separate text box for entering a search string and a 'Find' button. 它有一个单独的文本框,用于输入搜索字符串和“查找”按钮。 If the search string had changed since the last search, a button click meant a regular find, if not, it meant 'find again'. 如果自上次搜索后搜索字符串已更改,则单击按钮意味着定期查找,如果不是,则表示“再次查找”。 Here is the button handler: 这是按钮处理程序:

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 is set to true in the TextChanged handler of the search box. m_fTextIsNew在搜索框的TextChanged处理程序中设置为true。

Hope this helps. 希望这可以帮助。

Edit: added Body and Document properties 编辑:添加了正文和文档属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM