0

I am trying to write a code to download data from public data source to our server. In order to do that I wrote a code that opens the web page of data source website (WebBrowser Control named webBrowser1) then input the file number in the text box on the web page and click ok to get the result. Code worked fine for one file but then when I tried to do that in loop, InvokeMember("Click") only works when the loop is done (only clicks at the end of the loop). How can i change this code to click the button on webpage iteratively when Well_File_No changes? My code is attached below. Thanks for the help. I am writing the code using C# Windows Form.

 private void runToolStripMenuItem_Click(object sender, EventArgs e)
    {
        //Code runs from Toolstrip on the Windows Form
        for (int i = 0; i < 10; i++)
        {
            // this function try to locate textbox and click button on webpage 
            test(i);
         }

    }



    void test(int i)
    {
       // File Number to be entered in textbox named "fileno"
        Well_File_No = Convert.ToString(17141 + i);
            webBrowser1.Document.GetElementById("fileno").SetAttribute("Value", Well_File_No);

        foreach (HtmlElement btn in webBrowser1.Document.GetElementsByTagName("input"))
        {
            var BTN = btn;
            if (BTN.GetAttribute("type").Equals("submit"))
            {
                webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
                BTN.InvokeMember("Click");
                break;
            }
        }
        while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
        {
            Application.DoEvents();

        }
    }

      void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
          ... process data from the table and store....
     }
Amir
  • 625
  • 1
  • 11
  • 26
  • 1
    There is no obvious point to that while-loop. If there's any code after it then it should be moved into the DocumentCompleted event handler. Also the place where you'd do anything "iteratively", the next step anyway. You always need a variable to keep track of "steps". Google "async await documentcompleted" for a possibly better way to do this. – Hans Passant Jul 27 '16 at 16:38
  • Yes You are right that while loop has no effect. But the question is who can i force test(i) to execute InvokeMember("Click") every time value of "i" changes within the loop? – Amir Jul 27 '16 at 16:44

0 Answers0