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.
It is not a good solution to do a
for
or awhile
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:
Add a
Click
event on yourSubmit
button. When the user clicks onSubmit
, it calls the method that will process the nextlistBox
item:The method that processes the next
listBox
item:And call a method at the start (the user doesn't click on
Submit
before the firstlistBox
item is processed):