Selecting text in webrowser control

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • billhislop
    New Member
    • Apr 2010
    • 1

    #1

    Selecting text in webrowser control

    I am having trouble with some code that is intended to take a previously stored url and text selection within the page, open the web page in a webrowser control and then select the text.

    I have the first bit working and successfully navigate to the url, but I can't find a working solution for some code in the Navigated event handler to select the text. Most of the solutions I have seen on the internet get the SelectionObject for the page and then use the Move() member but my code to get a TxtRange from the SelectionObject causes an exception as the SelectionObject initially has type = none and the createRange() method only works when type = text.

    Relevant code shown below - can anyone help?

    Code:
    private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
    {
                try
                {
                    IHTMLDocument2 htmlDocument = webBrowser1.Document.DomDocument as IHTMLDocument2;
                    IHTMLSelectionObject currentSelection = htmlDocument.selection;
    
                    IHTMLTxtRange range = currentSelection.createRange() as IHTMLTxtRange;  // causes exception
    
                    range.moveToPoint(this.Start_X, this.Start_Y);
Working...