I want to use a MessageBox in a while loop with OK button. I want the MessageBox keeps showing until the user presses OK button. If I use this code the box shows up and it waits until the user presses the button and it does not proceed to the next iteration.
DialogResult dialogResult;
StopIteration = false;
While(!StopIteration)
{
nIteration += 1;
dialogResult = MessageBox.Show(string.Format("Iteration: {0}", nIteration), "Iterations", MessageBoxButtons.OK);
if (dialogResult == DialogResult.OK)
{
StopIteration = true;
}
}
Alternatively I made a form with a label to be modified during the loop:
public partial class RunMessageBox : Form
{
public RunMessageBox()
{
InitializeComponent();
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
}
}
RunMessageBox runMessageBox = new RunMessageBox();
runMessageBox.Show(); // If I use DialogShow instead the same problem as MessageBox happens
While(!StopIteration)
{
nIteration += 1;
runMessageBox.labelIteration.Text = string.Format("Iteration: {0}", nIteration);
if (runMessageBox.checkBoxStopIteration.Checked)
{
StopIteration = true;
}
}
This time the label and check box for stopping are not displayed until the last iteration (the code includes maximum iteration check).