1

I have a for loop that goes to a site and posts to its forms. For every item in the listbox I want it to wait for the user to fill out the data to the site, then move. The key point here is 'wait'.

So my question is: is it possible to make a for loop to wait for user input?

This is the for loop I am working in order to load the data to the forms:

if (webBrowser1.DocumentText.Contains("Welcome"))
{   
    for (int i = 0; i < listBox4.Items.Count; i++ )
    {

        listBox4.SetSelected(i, true);
        listBox5.SetSelected(i, true);
        //coded to submit to form

        Application.DoEvents();
    }
}

This is the code to click submit on the site:

Application.DoEvents();
foreach (HtmlElement webpageelement in allelements)
{

     if (webpageelement.GetAttribute("value") == "Submit")
     {
         webpageelement.InvokeMember("click");
         Application.DoEvents();

     }

 }

I've also tried making a for loop without the code in it to make it go on. For ex: i++ and make an if statement to make it go on but that lagged my interface.

6
  • Your question is really not clear. Please explain better what you are doing and what you wanna do. Commented Oct 28, 2011 at 22:14
  • @Otiel Alright. I have a listbox. In the listbox I can add items to it. For each item in the listbox my program goes to the site and fills predefined information to it. I want the program to wait for the user to fill out the captcha and them clicking the submit button on my interface before the for loop moves down to the next item in the list Commented Oct 28, 2011 at 22:29
  • Then use the solution proposed by @HansPassant. Open a new form containing your browser using Form.ShowDialog() in your for loop, and close this form when the user clicks on your Submit button. Your for loop will be paused until the user clicks on Submit. Commented Oct 28, 2011 at 22:54
  • But what if there will be several items in the listbox. Would opening and closing each and everytime be a good solution for the user and for the computer? Commented Oct 28, 2011 at 23:03
  • Also, I was asking around on another forum and someone recommended using a do while loop. Would this be a viable solution if I incorporate it with a for loop or should I stick with Form.ShowDialog? Commented Oct 28, 2011 at 23:05

1 Answer 1

1

It is not a good solution to do a for or a while loop to wait for user input. Don't do that. Your program will be constantly working while waiting for the condition to get it out of the loop. Instead, you should find a solution using events or else.

If you don't want to use the Form.ShowDialog() solution proposed in your question comments, you could come up with something like that:

Have a global variable that holds the index of the listBox item we are working on:

int currentItemIndex;

Add a Click event on your Submit button. When the user clicks on Submit, it calls the method that will process the next listBox item:

private void buttonSubmit_Click(Object sender, EventArgs e) {
    // Process next listBox item
    ProcessNextItem();
}

The method that processes the next listBox item:

private void ProcessNextItem() {
    currentItemIndex += 1;
    if (currentItemIndex >= listBox.Items.Count) {
        // We have gone through all listBox items

        // Do nothing
    } else {
        // Fill predefined information to the website
        website.SomeField = listBox.Items[currentItemIndex].SomeField; // Whatever you do to fill predefined information
}

And call a method at the start (the user doesn't click on Submit before the first listBox item is processed):

private void Start() {
    currentItemIndex = -1;
    ProcessNextItem();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.