Two parts of my program are a winforms WebBrowser and a simple TextBox.
What I want now is, when I select a text in the WebBrowser it automatically copies the selected text in the TextBox.
I could not find anything about this on google so I'd be glad if someone could help me!
You can attach an event handler to onselectionchange event of Document of the WebBrowser control using AttachEventHandler method of document. Then you can use properties of DomDocument to get selected text.
Example
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Document.AttachEventHandler("onselectionchange", selectionchange);
}
private void selectionchange(object sender, EventArgs e)
{
dynamic document = webBrowser1.Document.DomDocument;
dynamic selection = document.selection;
dynamic text = selection.createRange().text;
this.textBox1.Text= (string)text;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With