Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 272
jith10 Avatar asked Jan 14 '23 11:01

jith10


1 Answers

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.

like image 146
Behroz Sikander Avatar answered Jan 31 '23 10:01

Behroz Sikander