How do I COPY the selected text from a webBrowser page?

q12

Newcomer
Joined
Jun 24, 2009
Messages
10
This is a Hard question(apparently simple):
How do I COPY the selected text from a webBrowser page, and paste it to a text file( for example)?
I have this until now:

Code:
private void webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
//the 67 is the mousebutton no4 (I have 5 5buttons and a scrolwhell-A4Tech_X7 mouse) 
//the mousebutton no4 is set to "ctrl+c" keys (Copy) 
          if (e.KeyValue == 67)
          textBox2.Text=webBrowser1.DocumentText;
        }

Until now i cant manage to copy a text selection from webBrowser.
How to do that?
Thank you.
 
This code works just fine for me. What is the problem you are having? Is the event not being raised? Or are you not getting the keycode you are expecting?
Code:
        private void webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
            if (e.Modifiers == Keys.Control && e.KeyCode == Keys.C) {
                Clipboard.SetText(webBrowser1.DocumentText);
            }
        }
There are some potential problems with this approach, depending on what you're doing with this program. If I select text in a textbox within the browser and try to copy it, I end up getting the page's HTML on the clipboard instead of the text selected in the textbox. You'll want to be aware of quirks like this if it's something that might be a problem.
 
Back
Top