如何更改父母和孩子的形式 [英] how to change parent and child forms

查看:73
本文介绍了如何更改父母和孩子的形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是:form1是父级,而form2是子级.
所以当我关闭form1时,form2将自动关闭.

problem is:form1 is parent and form2 is child.
so when i close form1,form2 will close automatically.
how to change parent and child forms to each other?

推荐答案

这是因为应用程序的生命周期链接到Form1.用于启动应用程序的Main方法显示如下:

It''s because the lifetime of the application is linked to Form1. The Main method used to start the application shows this:

[STAThread]
static void Main()
{
  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  Application.Run(new Form1());
}



Application.Run在Form1()对象的生存期内保留主应用程序线程.如果关闭Form1,则线程退出,过程结束.

我的建议是添加Form3,它是用于管理表单的控制器.将其设置为不可见,但在构造函数中将Form1作为Form3的子项加载.

在需要Form2时向Form1添加一个事件,并使用Form3来打开Form2.下面的简短演示:



Application.Run Holds up the main application thread for the lifetime of the Form1() object. If you close Form1 the thread exits and the process ends.

My advice would be add Form3 which is a controller for managing the forms. Set it to invisible but in the constructor load Form1 as a child to Form3.

Add an event to Form1 for when Form2 is required and use Form3 to also open Form2. Brief demo below:

public class Form3 : Form
{

  [STAThread]
  static void Main()
  {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form3());
  }

  Form1 frm1;
  Form2 frm2;

  public Form3()
  {
    frm1 = new Form1();
    frm1.OnOpenForm2 += new Form2.OnOpenForm(frm1_OnOpenForm2);
    frm1.FormClosed += new FormClosedEventHandler(frm_FormClosed);
    frm1.Show();
  }

  private void frm1_OnOpenForm2()
  {
    frm2 = new Form2();
    frm2.FormClosed += new FormClosedEventHandler(frm_FormClosed);
    frm2.Show();
  }

  void frm_FormClosed(object sender, FormClosedEventArgs e)
    {
      if(((Form)sender) == frm1)
        frm1 = null;
      if(((Form)sender) == frm2)
        frm2 = null;
      if(frm1 == null && frm2 == null)
        Application.Exit();
    }

}

public class Form1 : Form
{
  public delegate void OpenForm2();
  public event OpenForm2 OnOpenForm2;
}



在项目属性中,您必须将起始对象更改为Form3.

它写在我的头上,因此,如果有任何问题或您不了解的任何内容,请告诉我.

由于您未显示表格3,因此其使用可能被称为不正确. Application.Run也将接受ApplicationContext类.有关ApplicationContext的更多信息,请参见此处:

http://msdn.microsoft.com/en-us/library/system. windows.forms.applicationcontext.aspx [ ^ ]



In your project properties you''ll have to change the start object to be Form3.

It''s written off the top of my head, so if there are any problems or anything you don''t understand, let me know.

As you''re not displaying form 3, it''s use could be called improper. Application.Run will also accepts an ApplicationContext class. For more information about ApplicationContext please see here:

http://msdn.microsoft.com/en-us/library/system.windows.forms.applicationcontext.aspx[^]


这篇关于如何更改父母和孩子的形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆