简体   繁体   English

调用静态方法以关闭表格

[英]Invoke static method to close form

I have two Windows Forms - Form1 and Form2 . 我有两个Windows窗体Form1Form2 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-tonMulti-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 . 可以有Form1Form2多个实例。 Consider the below situation. 考虑以下情况。

  • User opens Form1 , then opens Form2 from Form1 , in button click of Form2 , Form1 should be closed (INSTANCE 1) 用户打开Form1 ,然后从Form1打开Form2 ,在Form2按钮单击中,应关闭Form1 (实例1)
  • User can again open another instance of 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM