1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Break out of this loo[p

Discussion in 'C, C++, C#' started by kytro360, Aug 23, 2011.

  1. kytro360

    kytro360 Power Member

    Joined:
    Jan 12, 2010
    Messages:
    703
    Likes Received:
    732
    Basically I am trying to click enter on this site for a program I am working on. I asked on another forum and they supplied me with a foreach loop. The thing is the foreach loop keeps looping xD. I even added a break command to it but it still constantly clicks enter.

    Can you help me stop it?

    p.s: Its located in the Document_Completed event.

    Code:
     if (webBrowser1.Url.AbsoluteUri.Contains("http://www.mailinator.com/"))
                {
    
                    webBrowser1.Document.GetElementById("email").SetAttribute("value", txtMailName.Text);
                    
                    HtmlElementCollection inputs = webBrowser1.Document.GetElementsByTagName("input");
    
                    foreach (HtmlElement input in inputs)
                    {
                        if (input.GetAttribute("name") == "email")
                            input.Focus();
                        SendKeys.Send("{ENTER}");
                        break;
                    }
                }
     
  2. Senotaru

    Senotaru Registered Member

    Joined:
    Jan 17, 2011
    Messages:
    67
    Likes Received:
    11
    Code:
     
     if (webBrowser1.Url.AbsoluteUri.Contains("http://www.mailinator.com/"))
                {
    
                    webBrowser1.Document.GetElementById("email").SetAttribute("value", txtMailName.Text);
                    
                    HtmlElementCollection inputs = webBrowser1.Document.GetElementsByTagName("input");
    
                    foreach (HtmlElement input in inputs)
                    {
                        if (input.GetAttribute("name") == "email")
                        {
                            input.Focus();
                            SendKeys.Send("{ENTER}");
                         } else {                    
                            break;
                         }
                    }
                }
    Try this. If I understand your issue, its because the if statement doesn't have brackets, which means its going to only work on the first command after the if statement. Aka, if it found "email", it would set the focus, but the enter command was not included within the if statement, so every time the loop executed, the conditional was only taken into account for set focus. Which means that the sendkeys would be sent regardless of the conditional statement you've set.
     
    Last edited: Aug 25, 2011