alkampfer on February 13th, 2009

In windows forms the WebBrowser control permits to include a fully funcional browser into your application. The interesting things is that you can interact with the html of the site with no problem. As an example you can load a page and highlight some words into the text, here is the result of loading www.nablasoft.com and I want to highlight "laureati" and "passione".

image

As you can see I’ve highlighted the two words, the code is really simple.

private void button1_Click(object sender, EventArgs e) { webBrowser1.Navigate("http://www.nablasoft.com/"); webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted; } void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { IHTMLDocument2 doc2 = webBrowser1.Document.DomDocument as IHTMLDocument2; StringBuilder html = new StringBuilder(doc2.body.outerHTML); var words = new[] { "laureati", "passione" }; foreach (String key in words) { String substitution = "<span style='background-color: rgb(255, 255, 0);'>" + key + "</span>"; html.Replace(key, substitution); } doc2.body.innerHTML = html.ToString(); }

First of all you need to wait that the document finished to load before you can interact with the content, this is simple because you can use the event DocumentCompleted. To access content you should refer the MSHTML COM Library in your project since the webbrowser uses the mshtml internally. To highlight words I simply grab all content, surround the keywords with a simple span with a background style and then replace the whole html text again…really simple.

alk.

Tags:



kick it on DotNetKicks.com

14 Responses to “Highlight words in webbrowser control”

  1. Hello! I’m using the C# language, I have tried your coding and convert it into C#.

    My program haven’t run the code in documentcompleted eventhandler

    when it step to the first statement (HtmlDocument abc = (HtmlDocument)webBrowser1.Document.DomDocument;
    ), it step out

    Can you tell me which part i have caused the problem?

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
    HtmlDocument abc = (HtmlDocument)webBrowser1.Document.DomDocument;
    StringBuilder html = new StringBuilder(abc.Body.OuterHtml);

    string text = \school\;

    string substitution = \\ + text.ToString() + \\;

    html.Replace(text.ToString(), substitution);

    abc.Body.InnerHtml = html.ToString();
    }

  2. double quote is disappear, so it shows the \\

    string substitution = + text.ToString() + ;

  3. nice and simple . a simple step further to go seems to put a simple button and a textbox for search

    this is the code for the button

    private void button8_Click(object sender, EventArgs e)
    {
    //if containts >1 word
    if(searchKeywords.Length > 0)
    {
    //reverse the highlighted keywords if it only worked!!
    foreach(string keyword in searchKeywords)
    {
    var temp_str = “” + keyword + “”;
    webBrowser1.DocumentText = webBrowser1.DocumentText.Replace(temp_str, keyword);
    }
    webBrowser1.Invalidate();
    }
    //the textbox may have many word divided by char space
    var keywords = textBox2.Text.Split(‘ ‘);
    //grab the values for use to reverse highlighting
    searchKeywords = keywords;

    //highlight search keywords in webbroswer control
    foreach (string keyw in keywords)
    {
    //MessageBox.Show(index.ToString());
    webBrowser1.DocumentText = webBrowser1.DocumentText.Replace(keyw, @”"+keyw+”");
    }
    //control repaint
    webBrowser1.Invalidate();

    }

    but the reversing of highlighting doesn’t work . what do you think?

  4. Actually I used a javascript to hilite words as you can see in this post http://www.nablasoft.com/alkam.....f-control/ it works better than this example.

  5. private void RealBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
    //Watch list
    KeywordManager keys = new KeywordManager();
    string[] words = keys.GetKeywordList();
    try
    {

    mshtml.HTMLDocument doc2 = (mshtml.HTMLDocument)RealBrowser.Document.DomDocument;
    StringBuilder html = new StringBuilder(doc2.body.outerHTML);

    foreach (String key in words)
    {
    String substitution = “” + key + “”;
    html.Replace(key, substitution);

    doc2.body.innerHTML = html.ToString();
    }
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    }

    i use this and when i click on the highlighted HTML link it is not working

  6. As soon as possibile I’ll post a working sample, it seems that everything is ok, so I’ll try your code to understand what is wrong.

    alk.

  7. The problem with this kind of code, is that we manipulate directly the DOM in a very harsh way. As an example that code replaces key even inside tag, and sometimes this completely destroy the html page. The main problem is that you need an html parser to avoid HTML structure corruption.

    A better solution I found, as I said before, was switching to an alreaady tested javascript function to hilite text.

    To reverse hiliting the best way is to save the original content of the page, and when you need to remove hilighting you simply reassign old content to the innerHTML property.

    alk.

  8. Please convert this code to vb.net !

  9. Converting to VB.Net should not be a difficult task, but I suggest you to use my other approach listed in this post http://www.codewrecks.com/blog.....f-control/ that uses a javascript hiliter.

    Alk.

  10. I did your code above and it worked for me. My Question is, can i use outertext instead of outerhtml from the body so that i can highlight those text inside a html tag. like inside an “A” element. Sample is

    “This is a HighLight Me please sample”.

    My search word is “This is a HighLight Me please sample” and this should match the above exampe and ignoring the html element.

  11. Doing what you want is just a matter of creating a javascript that is able to do this. But it seems not simple to me. The javascript functino I used was taken from a third party blog.

    Alk.

  12. HighLight Results in WebBrowser Control for WPF …

    I nedd to do a little variation of this code to make it works in WPF, here is the code:

    1- I need to add the reference: Microsoft.mshtml (solution explorer / Referenes)

    2- add using mshtml;

    3- code for the event LoadCompleted (wbContenido = WebBrowser control)

    private void wbContenido_LoadCompleted(object sender, NavigationEventArgs e)
    {
    mshtml.HTMLDocumentClass dom = (mshtml.HTMLDocumentClass)wbContenido.Document;
    //Console.Write(dom.body.innerHTML);

    IHTMLDocument2 doc2 = dom;// wbContenido.Document.DomDocument as IHTMLDocument2;
    StringBuilder html = new StringBuilder(doc2.body.outerHTML);

    var words = new[] { “Examen”, “passione” };
    foreach (String key in words)
    {
    String substitution = “” + key + “”;
    html.Replace(key, substitution);
    }

    doc2.body.innerHTML = html.ToString();
    }

Trackbacks/Pingbacks

  1. background » Blog Archive » Highlight words in webbrowser control
  2. Hilite words in webbrowser now for WPF control

Leave a Reply