I have a web browser control embedded in a form. Upon form load it loads a locally stored HTML file. I have implemented a find text functionality to find a particular text in the HTML document loaded in the web browser control. It is working for finding the first occurrence of the word specified.
But I want to highlight all the occurrences of the specified word all at once or still better implementing something analogous to “Find Next” function found in various applications. Is it possible to do that for web browser control???
Here’s the current code:
private void toolStripButton1_Click(object sender, EventArgs e)
{
string TextToFind;
TextToFind = toolStripTextBox1.Text;
if (webBrowser1.Document != null)
{
IHTMLDocument2 doc = webBrowser1.Document.DomDocument as IHTMLDocument2;
if (doc != null)
{
IHTMLSelectionObject currentSelection = doc.selection;
IHTMLTxtRange range = currentSelection.createRange() as IHTMLTxtRange;
if (range != null)
{
String search = TextToFind.ToString();
if (range.findText(search, search.Length, 2))
{
range.select();
}
}
}
}
}
Thanks.
You will find the the code sample for your question here MSDN Forums: WebBrowser Find Dialog
Hope that is exactly what you are looking for.