WebBrowser 2005 Document.Selection

I was using webbrowser.document.selection on a webbrowser control in VS.net 2003, and now I have upgraded to 2005, and the selection object is not there. Is there any easy way to get the selected text from the webbrowser control without having to copy the text to the clipboard

Thanks,

- Jon



Answer this question

WebBrowser 2005 Document.Selection

  • R.Tutus

    Is there anyway to know when the text is selected

  • ClaudiaHelpOnVSTO

    Attaching (and detaching) an event handler is fairly easy... not worse than what you would normally do for a regular event in C#. Consider the following (I'll assume you have a WebBrowser control named "webBrowser1" and a TextBox named "textBox1". Copy the GetSelection method from my other post in this thread.

    private void OnSelectionChange (object sender, EventArgs e) {
    textBox1.Text = GetSelection (webBrowser1);
    }

    private void webBrowser1_DocumentCompleted (object sender, WebBrowserDocumentCompletedEventArgs e) {
    webBrowser1.Document.AttachEventHandler ("onselectionchange", new EventHandler (OnSelectStart));
    }

    This will fill "automagically" the content of the TextBox with whatever you select in the HTML document.

    That's all. Remember that you should not attach an event before you get the DocumentCompleted event, and try to detach the event when you no longer need it, or you risk having events piling up.

    AttachEventHandler is exposed also by HtmlElement, which means that you can attach to events that are specific for any HTMl element in the DOM. Keep in mind that the set of events available to documents and elements can be quite different...

    Now, the real issue is which event you should track. The one I used before will fire whenever you start a selection, and there is another event, onselectionchange, that will fire continuously while you are selecting. Which one is best depends on what you are trying to track.

    HTH
    --mc


  • WII

    I don't know if there is a more direct way, but you can still access the underlying interfaces, so this will only require a few more lines of code.

    1) Add a reference to MSHTML (in the COM tab of the Add Reference dialog, find Microsoft HTML Object Library).

    2) Add "using mshtml;".

    private string GetSelection (WebBrowser webBrowser) {
    IHTMLDocument2 doc = (IHTMLDocument2) webBrowser.Document.DomDocument;
    IHTMLSelectionObject sel = doc.selection;
    IHTMLTxtRange range = (IHTMLTxtRange) sel.createRange ();
    return range.text;
    }

    If you don't like having the additional reference, you may consider injecting a small script into the document and then call that via InvokeScript.

    HTH
    --mc


  • m_nille

    and how to select all text in webBrowser control Thanks
  • scott gallimore

    Well you can still use the interfaces of MSHTML.

    There are several options, that include the possibility to search up or down, in the current selection only, match whole word... the works.

    I will stick to the most basic situation, in which you might need to implement a FindFirst and a FindNext. The two are very similar, with the only difference that the first method will always start from the beginning of the document, while the second will continue searching from the current position. Here goes:

    private bool FindFirst (WebBrowser webBrowser, string text) {
    IHTMLDocument2 doc = (IHTMLDocument2) webBrowser.Document.DomDocument;
    IHTMLSelectionObject sel = (IHTMLSelectionObject) doc.selection;
    sel.empty (); // get an empty selection, so we start from the beginning
    IHTMLTxtRange rng = (IHTMLTxtRange) sel.createRange ();
    if (rng.findText (text, 1000000000, 0)) {
    rng.select ();
    return true;
    } else {
    return false;
    }
    }

    private bool FindNext (WebBrowser webBrowser, string text) {
    IHTMLDocument2 doc = (IHTMLDocument2) webBrowser.Document.DomDocument;
    IHTMLSelectionObject sel = (IHTMLSelectionObject) doc.selection;
    IHTMLTxtRange rng = (IHTMLTxtRange) sel.createRange ();
    rng.collapse (false); // collapse the current selection so we start from the end of the previous range
    if (rng.findText (text, 1000000000, 0)) {
    rng.select ();
    return true;
    } else {
    return false;
    }
    }

    As you may see, the only difference in the two mehtods is in the lines highlighted in red. Let's see briefly what they do:

    In the first case we want to get rid of the current selection. This means that when we create the range in the following line, we get a "degenerate range" starting (and ending) at position 0 in the document. This way, we will search from the beginning.

    In the second case, we want to continue our search from the current position. The problem is that if we just called FindFirst, the current selection already contains the word we are searching and we don't want to find that one again. So we collapse the range instead, which will move the start position up to the end position of the current selection. This way, our search will continue. If we passed true to collapse, things would go the other way. You will need to do that if you intend to search up instead of down.

    Finally the main method, i.e. findText. There are several options, that would be long to expose here. Better look up IHTMLTxtRange.findText on the MSDN.

    HTH
    --mc


  • NorCis

    Thanks for the help, also is there any way to select some specific text in the webbrowser
  • hazz

    Is is possible to know what word in the document is click. For example, I have a document that is displayed in my web control, and when a user clicks on a certain word, I would like it to be highlighted, or I would like to know what word it is. Is this at all possible

    Thanks,

    - Jon


  • Raja Pratap

    Mario....while I have you here...

    I have a vertical band object that I cant hang onto the eDisp...whenever a new window is opened in the browser...it "attaches" to the original....and when closed....fires my close event in the original. I am playing/recording an mp3...In in the quit event I clean up (stop recording etc)...so its quite a kink in the project.

    Now with IE7...its even worse....whenever a tab is closed...same thing.

    Sorry to go off topic here...but this is driving me crazy.



  • WebBrowser 2005 Document.Selection