Results 1 to 4 of 4

Thread: [RESOLVED] get selected text from webbrowser1

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Posts
    534

    Resolved [RESOLVED] get selected text from webbrowser1

    i'm useing WebBrowser1 and the code i use to get the text is
    vb Code:
    1. Dim docx As HtmlDocument = WebBrowser1.Document
    2.         Dim readme As Object = Nothing
    3. docx.ExecCommand("copy", True, readme)

    i want to make richtextbox that retrieve the selected text without the copy paste method.i want it to be sensitive to the mouse click so it can retrieve the selected text. can some one help me with the code

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Posts
    534

    Re: get selected text from webbrowser1

    ok i done it this way
    vb Code:
    1. Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
    2.         Dim htmlDocument As mshtml.IHTMLDocument2 = TryCast(WebBrowser1.Document.DomDocument, mshtml.IHTMLDocument2)
    3.         Dim currentSelection As mshtml.IHTMLSelectionObject = htmlDocument.selection
    4.         If currentSelection IsNot Nothing Then
    5.             Dim range As mshtml.IHTMLTxtRange = TryCast(currentSelection.createRange(), mshtml.IHTMLTxtRange)
    6.             If range IsNot Nothing Then
    7.                 MessageBox.Show(range.text)
    8.             End If
    9.         End If
    10.     End Sub
    any one know how to make it work with mouse click?

  3. #3
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: get selected text from webbrowser1

    Quote Originally Posted by new1 View Post
    any one know how to make it work with mouse click?
    Maybe add a mouse up event handler, something like,..

    Code:
    Private Sub WebBrowser1_DocumentCompleted(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        If Not (WebBrowser1.Document Is Nothing) Then
            WebBrowser1.Document.AttachEventHandler("onmouseup", AddressOf wb_OnMouseUp)
        End If
    End Sub
    
    Private Sub wb_OnMouseUp(ByVal sender As Object, ByVal e As EventArgs)
        If (WebBrowser1.Document Is Nothing) Then Return
        Dim htmlDocument As mshtml.IHTMLDocument2 = TryCast(WebBrowser1.Document.DomDocument, mshtml.IHTMLDocument2)
        Dim currentSelection As mshtml.IHTMLSelectionObject = htmlDocument.selection
        If currentSelection IsNot Nothing Then
            Dim range As mshtml.IHTMLTxtRange = TryCast(currentSelection.createRange(), mshtml.IHTMLTxtRange)
            If range IsNot Nothing Then
                Try
                    If range.text.Trim.Length > 0 Then
                        ' add selected text to RTB
                        RichTextBox1.AppendText(range.text & Environment.NewLine)
                    End If
                Catch
                End Try
            End If
        End If
    End Sub

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Posts
    534

    Re: get selected text from webbrowser1

    why the append text in the line
    ""vb Code:
    1. RichTextBox1.AppendText(range.text & Environment.NewLine)
    double the result i mean it type the word twice

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width