Webbrowser - Find what word was clicked

Is it possible to find what word a user clicks when he/she clicks inside of a webbrowser control, using C# I would like to highlight/select the word that is clicked, or at least know what the word is, so I can search my database for possible definitions. I would like it to be like when you double click a word in internet explorer (on a webpage), and it selects the word, but I only want the user to have to do it in one click. I thought about converting all my words to hyper-links, and then getting the active element from the Document, but I think that will take too much processing power for lengthy data displays. Any ideas will be helpful.

Thanks,

- Jon



Answer this question

Webbrowser - Find what word was clicked

  • Bazzer

    For anybody who wants to know, I found the answer to my problem.  With a little fiddling around, and help from MSDN, I found that you can select words with one click by using the following code, on the onmouseup event:

    IHTMLDocument2 doc = (IHTMLDocument2)webMain.Document.DomDocument;

    IHTMLSelectionObject sel = (IHTMLSelectionObject)doc.selection;

    IHTMLTxtRange rng = (IHTMLTxtRange)sel.createRange();

    rng.expand("word");

    rng.select();

    This will expand the selection by selecting the 'word', you can also do 'sentence', or 'character' (just replace "word").  Then you can just get the selection by doing:

    return rng.text;

    And there you have it getting the selected word. (You could collapse the range after you get the selected text, if you don't want the word to be selected.)

    Thanks,

    - Jon


  • GKW82

    Yes, I know, I started that thread, and asked the same question, but nobody replied. Thanks though.
  • hiner129

    There recently was a subject covered on handling the selected text of a WebBrowser control on this forum. Perhaps it will help you:
    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=851468&SiteID=1


  • Webbrowser - Find what word was clicked