jith10
jith10

Reputation: 553

Simulating a Copy Paste on a WebBrowser Control in Win Forms

I have a WebBrowser control in a form with textual data and in that form I am introducing 2 buttons. One to copy the entire contents(CTRL+A, CTRL+C) and the other to copy only the selected (using a mouse) text i.e. only a CTRL+C and then paste it to a notepad.

Code for Copy: (this works partially correct. copies only upto a certain point)

this.WebBrowser.Document.Focus();
SendKeys.SendWait("^a");
SendKeys.SendWait("^a^c");
this.WebBrowser.Refresh();

Code for Copy Selected: (this does not work at all)

this.WebBrowser.Document.Focus();
SendKeys.SendWait("^c");
this.WebBrowser.Refresh();

Can you please tell me if this is the right way?

Upvotes: 4

Views: 4363

Answers (1)

Behroz Sikander
Behroz Sikander

Reputation: 4049

Try this for Copy:

this.WebBrowser.Document.ExecCommand("Copy", False, vbNull)

Or Use the property WebBrowser.IsWebBrowserContextMenuEnabled = True. This will enable the Context menu in the control from which you can Copy/Paste.

Upvotes: 9

Related Questions