Question

I'm have a Form1 and when I run a loop I need to open another form(Form2). The problem is that when I run the loop it shows the Form2 only for the first time, then the Form2 is opened and close automatically.

To probe it I wrote this simple code but it doesn't work:

Form2 reg = new Form2();
while (true) 
{
    reg.ShowDialog();
}

In Form2:

private void button1_Click(object sender, EventArgs e)
{
    Application.Exit();
}

Thanks for any help!

Was it helpful?

Solution

I think that you are closing your Form2 using the button1. This executes button1_click which doesn't simply close Form2, it closes the whole Application.

I tried your code and if I close Form2 using the X in the upper right corner it is closed and immediately reopened.

Should this be your problem, you can solve it by just modifying your method like this:

private void button1_Click(object sender, EventArgs e)
{
    this.Close();
}

Another solution could be setting the DialogResult property of your button to something different from the default DialogResult.None. In this way when the button is clicked the form is automatically closed, and the value of the property is used as the result of your ShowDialog().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top