![](/img/trans.png)
[英]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.