我如何编程选择WebBrowser控件的文本? C# [英] How Do I programatically select text in a webBrowser Control? c#
问题描述
下面的问题:
我想启用我的程序的用户搜索某个关键字(标准按Ctrl + F)WebBrowser控件。我不具有问题找到关键字在文档中并突出使用跨度的所有实例和替换()函数。 我上午有麻烦了查找下一个functionlity,我要工作。当用户点击查找下一个我想要的文件滚动到下一个实例。如果我能得到一个边框我可以使用导航功能。我用下面的code在丰富的文本框此相同的功能的工作。
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). I am having no problem finding the keyword in the document and highlighting all the instances using a span and the replace() function. I am having trouble getting the "find next" functionlity that I want to work. 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();
任何人都可以提供一种方法来得到这个在web浏览器的工作?
Can anyone provide a methodology to get this to work in a webBrowser?
推荐答案
我实现在一个WinForms应用程序,有一个嵌入式Web浏览器控件的搜索功能。它有一个单独的文本框用于输入搜索字符串和查找按钮。如果在最近的搜索搜索字符串就变了,点击一个按钮意味着一个普通的发现,如果没有,这意味着重新找到。这里是按钮处理程序:
I implemented a search feature in a WinForms app that had an embedded Web browser control. 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设置为true,在搜索框的框TextChanged处理程序。
m_fTextIsNew is set to true in the TextChanged handler of the search box.
希望这有助于。
编辑:增加了身体和文档属性。
added Body and Document properties
这篇关于我如何编程选择WebBrowser控件的文本? C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!