按OK后如何关闭工作表单

时间:2021-03-04 15:53:04

I am writing this part of code in form2 and want that if i successfully registered and i press OK button then form2 should also close along with it

我在form2中编写这部分代码,并希望如果我成功注册并按下OK按钮,那么form2也应该随之关闭

 if (MessageBox.Show("Registered Successfully") == DialogResult.OK)
  {
    Reg_Form f2 = new Reg_Form();
    f2.Close();
  }

3 个解决方案

#1


3  

You should not create a new form and close that. You should close the current form. If your code is in form2 as you write, you can reference the current form instance with this:

您不应该创建新表单并关闭它。你应该关闭当前的表格。如果您在编写代码时使用form2,则可以使用以下代码引用当前表单实例:

if (MessageBox.Show("Registered Successfully") == DialogResult.OK)
{
    this.Close();
}

this can be omitted for brevity, but I wanted to include it in the example to better explain the point - you need to call close on the current instance. The shorter form is simply:

为简洁起见,这可以省略,但我想在示例中包含它以更好地解释这一点 - 您需要在当前实例上调用close。较短的形式是:

Close();

#2


0  

Just use this.Close you don't need to create new instance

只需使用此。关闭您不需要创建新实例

if (MessageBox.Show("Registered Successfully") == DialogResult.OK)
{
    this.Close()
}

#3


0  

You can close it using this keyword

您可以使用此关键字将其关闭

if (MessageBox.Show("Registered Successfully") == DialogResult.OK)
  {
    this.Close();
  }

The this keyword refers to the current instance of the class and is also used as a modifier of the first parameter of an extension method.

this关键字引用类的当前实例,并且还用作扩展方法的第一个参数的修饰符。

#1


3  

You should not create a new form and close that. You should close the current form. If your code is in form2 as you write, you can reference the current form instance with this:

您不应该创建新表单并关闭它。你应该关闭当前的表格。如果您在编写代码时使用form2,则可以使用以下代码引用当前表单实例:

if (MessageBox.Show("Registered Successfully") == DialogResult.OK)
{
    this.Close();
}

this can be omitted for brevity, but I wanted to include it in the example to better explain the point - you need to call close on the current instance. The shorter form is simply:

为简洁起见,这可以省略,但我想在示例中包含它以更好地解释这一点 - 您需要在当前实例上调用close。较短的形式是:

Close();

#2


0  

Just use this.Close you don't need to create new instance

只需使用此。关闭您不需要创建新实例

if (MessageBox.Show("Registered Successfully") == DialogResult.OK)
{
    this.Close()
}

#3


0  

You can close it using this keyword

您可以使用此关键字将其关闭

if (MessageBox.Show("Registered Successfully") == DialogResult.OK)
  {
    this.Close();
  }

The this keyword refers to the current instance of the class and is also used as a modifier of the first parameter of an extension method.

this关键字引用类的当前实例,并且还用作扩展方法的第一个参数的修饰符。