.NET Web Browser Control: How to check if there is text selected??

Hi!


Im using the .NET Web Browser control. How can I check if there is text selected in the current loaded document

Matt



Answer this question

.NET Web Browser Control: How to check if there is text selected??

  • jwagner20

    Look into the web browser controls DocumentText class.


  • ckoest

    actually, you don't even have to go to that trouble.

    browser.Document.DomDocument.selection.type will return a string:

    "None" if there is no selection and "Text" et. al. if something is selected.



  • coln

    something like this, but i'd urge you to look up the documentation on MSDN:

    IHTMLDocument2 doc = webBrowser1.Document.DomDocument as IHTMLDocument2;

    IHTMLTxtRange range = doc.selection.createRange() as IHTMLTxtRange;

    range.findText("texttofind", 0, 0);

    ========

    you should look up the valid param values on msdn.

    but something like this should work.

    BTW - don't forget to mark posts as the answer if they answered your question



  • DanUp

    After looking around for a bit, I am not sure that this is even possible with the built in .NET functions. If it was, there would be, (logically anyway), a SelectedText function in the webBrowser class, (which there is not).

    Do some searching on MSDN, CodeProject and CodeGuru to see if you can come up with anything.


  • startlet

    you'll have to use the active x stuff to get to this functionality.

    you'll have to add a reference to MSHTML in your project.

    this is under the 'Add Reference->COM' tab, under 'Microsoft Html Object Library'.

    IHTMLDocument2 doc = webBrowser1.Document.DomDocument as IHTMLDocument2;

    IHTMLTxtRange range = doc.selection.createRange() as IHTMLTxtRange;

    MessageBox.Show(range.text);

    that should do the trick.



  • MarilynJ

    OK - webBrowser1.Document. What do I use to check if there is text selected

    Matt


  • Jing-ta

    One more thing:

    Is there anyway possible to code a Find feature

    Matt

  • .NET Web Browser Control: How to check if there is text selected??