|
-
Feb 10th, 2012, 05:12 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] get selected text from webbrowser1
i'm useing WebBrowser1 and the code i use to get the text is
vb Code:
Dim docx As HtmlDocument = WebBrowser1.Document
Dim readme As Object = Nothing
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
-
Feb 11th, 2012, 03:17 PM
#2
Thread Starter
Fanatic Member
Re: get selected text from webbrowser1
ok i done it this way
vb Code:
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
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
MessageBox.Show(range.text)
End If
End If
End Sub
any one know how to make it work with mouse click?
-
Feb 11th, 2012, 08:36 PM
#3
Re: get selected text from webbrowser1
 Originally Posted by new1
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
-
Feb 12th, 2012, 03:57 AM
#4
Thread Starter
Fanatic Member
Re: get selected text from webbrowser1
why the append text in the line
""vb Code:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|