Break out of this loo[p

kytro360

Power Member
Joined
Jan 12, 2010
Messages
708
Reaction score
737
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;
                }
            }
 
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:
Back
Top