[英]Invoke static method to close form
I have two Windows Forms - Form1
and Form2
. 我有两个Windows窗体Form1
和Form2
。 I need to close Form1
from a button click event in Form2
. 我需要从Form2
的按钮单击事件关闭Form1
。 How do I do it if the method in Form1
is static
? 如果Form1
的方法是static
该怎么办?
Form1 表格1
namespace WinClose
{
public partial class Form1 : Form
{
public static void CloseForm()
{
this.Close();
}
}
}
Form2 表格2
namespace WinClose
{
public partial class Form2 : Form
{
private void button1_Click(object sender, EventArgs e)
{
Form1.CloseForm();
}
}
}
I am getting the below error 我收到以下错误
Keyword "this" is not valid in static property, static method or static field initializer. 关键字“ this”在静态属性,静态方法或静态字段初始化器中无效。
I have heard of Single-ton
, Multi-ton
concepts. 我听说过Single-ton
, Multi-ton
概念。 Is there a need to convert the class or is there any easy way to handle this? 是否需要转换类或有任何简单的方法来处理此问题?
EDIT 编辑
There can be multiple instances of Form1
and Form2
. 可以有Form1
和Form2
多个实例。 Consider the below situation. 考虑以下情况。
Form1
, then opens Form2
from Form1
, in button click of Form2
, Form1
should be closed (INSTANCE 1) 用户打开Form1
,然后从Form1
打开Form2
,在Form2
按钮单击中,应关闭Form1
(实例1) Form1
, then opens Form2
from Form1
, in button click of Form2
, Form1
should be closed. 用户可以再次打开Form1
另一个实例,然后从Form1
打开Form2
,在单击Form2
按钮中,应关闭Form1
。 (INSTANCE 2) (实例2) ie, When the user clicks button in Form2
(INSTANCE 2) , only one Form1
(INSTANCE 2) should be closed. 即,当用户单击Form2
按钮(INSTANCE 2)时 ,仅应关闭一个Form1
(INSTANCE 2) 。
Any help would be appreciated. 任何帮助,将不胜感激。
Thanks in advance. 提前致谢。
If you want to close only the Form1 that opened Form2 , you must pass Form1 as a parameter to Form2's constructor: 如果只想关闭打开Form2的Form1,则必须将Form1作为参数传递给Form2的构造函数:
namespace WinClose
{
public partial class Form2 : Form
{
Form1 form1;
public Form2(Form1 form1) {
InitializeComponent();
this.form1 = form1;
}
private void button1_Click(object sender, EventArgs e)
{
this.form1.Close();
}
}
}
If you want to close all instances of Form1: 如果要关闭Form1的所有实例:
namespace WinClose
{
public partial class Form2 : Form
{
private void button1_Click(object sender, EventArgs e)
{
foreach (var form in Application.OpenForms.OfType<Form1>().ToList())
form.Close ();
}
}
}
You obviously can't have a this
reference in a static method. 您显然不能在静态方法中使用this
引用。 Which instance would it be referring to? 它指的是哪个实例?
You can make it a singleton, but this sounds like a design smell. 您可以将其设置为单例,但这听起来像设计气味。
I see no reason the method should be static. 我看不出该方法应该是静态的。 If Form2
needs to know about Form1
, then it should have a reference to a Form1
instance. 如果Form2
需要了解Form1
,则它应具有对Form1
实例的引用。
Alternatively, if Form2
should not know about Form1
, but Form1
knows about Form2
, then Form2
can fire an event which signals Form1
to close. 或者,如果Form2
不应该知道Form1
,但是Form1
知道Form2
,则Form2
可以触发一个事件,该事件向Form1
发出信号。
Overthinking, close it directly. 想得太多,直接关闭它。 An instance of a form cannot be closed by a static method, since the class (static methods are part of the class) doesn't know about the instance. 表单的实例无法通过静态方法关闭,因为类(静态方法是该类的一部分)不了解实例。
namespace WinClose
{
public partial class Form2 : Form
{
Form1 form1;
private void button1_Click(object sender, EventArgs e)
{
this.form1.Close();
}
}
}
Value form1
can be set via the constructor or via a property from the caller. 可以通过构造函数或调用方的属性来设置值form1
。
You are calling in wrong way 你打错电话了
check following 检查以下
namespace WinClose
{
public partial class Form1 : Form
{
//no need of CloseForm
}
}
namespace WinClose
{
public partial class Form2 : Form
{
private void button1_Click(object sender, EventArgs e)
{
//Form1 o = new Form();//instance needed
Form1 o= Application.OpenForms.OfType<Form1>
if(o != null)
{
o.Close();
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.