I implemented a search function in a WinForms application with a built-in web browser. He had a separate text field for entering the search string and the Find button. If the search bar has changed since the last search, pressing the button means a normal find, and if not, it means "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) {
}
}
}
}
private DispHTMLDocument Document {
get {
try {
if (m_browser.Document != null) {
return (DispHTMLDocument) m_browser.Document;
}
} catch (InvalidCastException) {
}
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.
Hope this helps.
Edit: body and document properties added
source
share