0

我有一个 Form1,当我运行一个循环时,我需要打开另一个表单(Form2)。问题是,当我运行循环时,它仅第一次显示 Form2,然后 Form2 会自动打开和关闭。

为了探测它,我编写了这个简单的代码,但它不起作用:

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

在表格 2 中:

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

谢谢你的帮助!

4

1 回答 1

1

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().

于 2012-05-24T07:08:07.883 回答