[英]How to get the value of a SELECT HtmlElement in C# webBrowser control
[英]How Do I programatically select text in a webBrowser Control? c#
这是问题:我想让我的程序用户在webBrowser控件中搜索给定的关键字(标准Ctrl + F)。 我在文档中找到关键字并使用span和replace()函数突出显示所有实例都没有问题。 我有麻烦了“查找下一个” functionlity,我要工作。 当用户单击“查找下一个”时,我希望文档滚动到下一个实例。 如果我能得到一个边界框,我可以使用导航功能。 我使用以下代码在富文本框中使用相同的功能
//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应用程序中实现了搜索功能。 它有一个单独的文本框,用于输入搜索字符串和“查找”按钮。 如果自上次搜索后搜索字符串已更改,则单击按钮意味着定期查找,如果不是,则表示“再次查找”。 这是按钮处理程序:
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。
希望这可以帮助。
编辑:添加了正文和文档属性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.