I'm using Visual Studio Community in C# (.Net 4.5). I have a simple form, with one button and one webBrowser control. I want to check if "tremblay jean" has a trademark registered in his name in Canada (I know he has two). So when I click my button I load the trademarks search page in my webBrowser control, I wait for it to be complete, then I insert his name in their textbox and click their button. If I pause the program using a MessageBox.Show after loading the page, it works, there's two documents found. But if I don't pause the program using a MessageBox it doesn't work. It gives me 500 results, unrelated to "tremblay jean". So the line of code waiting for the ReadyState to be Complete doen't seem to work. Does anyone know why?
private void button1_Click(object sender, EventArgs e)
{
string website = "http://www.ic.gc.ca/app/opic-cipo/trdmrks/srch/home?lang=eng";
webBrowser1.Navigate(website);
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete) Application.DoEvents();
MessageBox.Show(webBrowser1.ReadyState.ToString()); // to pause the program
webBrowser1.Document.GetElementById("search-crit-1").SetAttribute("value", "tremblay jean");
HtmlElementCollection elc = webBrowser1.Document.GetElementsByTagName("button");
foreach (HtmlElement el in elc)
{
if (el.GetAttribute("type").Equals("submit"))
{
if (el.InnerText == " Search ")
{
el.InvokeMember("Click"); //comment this line to see if textbox is filled
break;
}
}
}
}