views:

196

answers:

3

I have a simple C# WinForms Crud AP for employees that have to do a lot of simple web lookups. Currently it loads 5 websites and puts a search term into the clipboard. The employee then has to paste the search term into the search textbox on each of the sites and hit enter. Is there a simple way to enter the data for them? The search textbox usually has focus when the sites load, I just cannot find a way to detect when the page has loaded or how to send the data to the window.

It looks, from these helpful answers, like I was going about this the wrong way. I was using code found with the help of my good friend, Mr. Google

System.Diagnostics.Process.Start("www.website.com");
Clipboard.SetText(textBox1.Text, TextDataFormat.Text);

I will do some more research this week on the webbrowser control

+1  A: 

You should be able to hook into the Webbrowser's Navigated event that should tell you when the page is Loaded (I THINK! I'm not 100% sure). As for setting the text, if you're sure that the textbox has focus you can do something like this:

        Clipboard.SetText("Stackoverflow");
        this.webBrowser1.Document.ExecCommand("Paste", false, null);
BFree
+2  A: 

You can use WebBrowser's DocumentCompleted event to be notified when the page has finished loading.

If the textbox you're entering your text into has a unique id, you can use WebBrowser.Document.GetElementById:

webBrowser.Document.GetElementById("some_id").InnerText = theSearchText;

GetElementById returns an instance of HtmlElement (or null, if the element wasn't found). Note that ".InnerText" might not work for all element types, there's also InnerHtml and lots of other properties, I'd recommend playing around with the HtmlElement class before deciding how to use it.

Let me know if you don't have a unique id for the element, there's lots of otherways to find an element. including using the exposed DOM via Document.Body.

Matthew Brindley
A: 

The WebBrowser's DocumentCompleted event is fired when a page (or a frame!) is loaded. When a page has frames, the DocumentCompleted event fires multiple times. In the DocumentCompleted event you compare the ReadyState property with ReadyState.Completed to check if it is fully loaded.

But in general, using SendKeys is not the best way to do this. Matthew Brindley's answer (to use GetElementById) is the best way, although this does not work for File input controls.

horsedrowner