0

I am creating a c# window form application, where a user can register to my website. The program is destined to ease the process of multiple registration from desktop. I have got 2 events for registration process.

First event :

 private void textBox5_TextChanged(object sender, EventArgs e)
        {


            if (textBox5.Text.Length == 4)
            {
                // users to enter captcha in this field
                button5.Text = "&Copy " + Convert.ToString( listBox2.Items.Count) + " Id";
                registerPost();              

                textBox5.Clear(); //clear the captcha for loading next captcha

            }

        }

Second event :

private void webBrowser2_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            validation();
            captcha();

         }

Now, the validation method contains, several logic :

    private void validation()
    {
        if (webBrowser2.DocumentTitle.Contains("Referral via Email"))
        {
            HtmlElementCollection classButton = webBrowser2.Document.All;
            foreach (HtmlElement element in classButton)
            {
                if (element.GetAttribute("className") == "cta-join-btn")
                {
                    element.InvokeMember("click");
                }

            }

        }

        else if (webBrowser2.DocumentText.Contains("Security Code does not match."))
        {
            label9.Text = "&Status : Wrong Captcha";
        }
        else if (webBrowser2.DocumentText.Contains("exceeded"))
        {
            label9.Text = "&Status : Exceeded";


            if (listBox1.SelectedIndex < listBox1.Items.Count - 1)
            {
                listBox1.SelectedIndex = listBox1.SelectedIndex + 1;
            }
            webBrowser2.Navigate(textBox4.Text);
        }
        else if (webBrowser2.DocumentText.Contains("Oops, you took too long to fill up this field. A new code has been generated for you."))
        {
            label9.Text = "&Status : Captcha Expired";
        }
        else if (webBrowser2.DocumentText.Contains("Username is taken."))
        {
            label9.Text = "&Status : UserName Exist";
            textBox3.Text = generateRandomID(Convert.ToInt32(textBox6.Text));
        }
        else if (webBrowser2.DocumentText.Contains("The email address you entered is already linked to an existing mig33 account! Please enter a different email address."))
        {
            label9.Text = "&Status : Email Exist";

            if (listBox1.SelectedIndex < listBox1.Items.Count - 1)
            {
                listBox1.SelectedIndex = listBox1.SelectedIndex + 1;
            }
            webBrowser2.Navigate(textBox4.Text);
        }
        else if (webBrowser2.DocumentTitle.Contains("Success")) //problem area 
        {

            writeText();
            label9.Text = "&Status : Success!!!";
            textBox3.Text = generateRandomID(Convert.ToInt32(textBox6.Text));

            if (listBox1.SelectedIndex < listBox1.Items.Count - 1)
            {
                listBox1.SelectedIndex = listBox1.SelectedIndex + 1;
            }
            webBrowser2.Navigate(textBox4.Text);
        }
    }

In my program there are two listBox, where listBox contains displays the ids and password that are created during the execution of each documentcomplete event and second listbox contains the registration links which have to be opened in the webbrowser control.

Every thing works fine, except that of conditional success in validation() method.

When the program is executed and and user starts registration process, and if user successfully creates a ID, listbox items are fired twice. maybe because the if the user successfuly registered, he is redirected to another page, arising the complication for documentcomplete event.

I am missing something somewhere, tried a lot, Any help will be highly appreciated.

sauk
  • 55
  • 8
  • possible duplicate of [DocumentCompleted firing multiple times - accepted StackOverflow answer not working](http://stackoverflow.com/questions/18321872/documentcompleted-firing-multiple-times-accepted-stackoverflow-answer-not-work) – noseratio Apr 04 '14 at 11:00

1 Answers1

0

@ sauk, Document event will remain interactive when it will not completed properly so you can use

private void webBrowser2_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {  
       // check event state is complete or interactive
        if (webBrowser2.ReadyState != WebBrowserReadyState.Complete)
             return;
            validation();
            captcha();

         }

your functions(validation() and captcha()) will only call when document complete event is completed. otherwise it will not call your functions

Aman
  • 2,196
  • 2
  • 17
  • 19