Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何为show form创建一个通用函数?_C#_Winforms - Fatal编程技术网

C# 如何为show form创建一个通用函数?

C# 如何为show form创建一个通用函数?,c#,winforms,C#,Winforms,我通常使用以下代码来显示表单: frmEmployeeManage em = null; private void ShowEmployee_Click(object sender, EventArgs e) { if (em == null || em.IsDisposed) { em = new frmEmployeeManage(); em.MdiParent = this;

我通常使用以下代码来显示表单:

frmEmployeeManage em = null;
private void ShowEmployee_Click(object sender, EventArgs e)
    {

        if (em == null || em.IsDisposed)
        {
            em = new frmEmployeeManage();
            em.MdiParent = this;
            em.FormBorderStyle = FormBorderStyle.None;
            em.WindowState = FormWindowState.Maximized;
            em.Show();
        }
        else
        {
            em.Activate();
        }
    }
现在我想写一个显示表单的函数。下面的代码我不知道如何将表单类作为参数传递给函数

class CommonService
{
  public static void ShowFrom(Form frmChild, Form frmParent)
  {
    if (frmChild == null || frmParent.IsDisposed)
    {
        frmChild = new Form(); // How passing the form class here?
        frmChild.MdiParent = frmParent;
        frmChild.FormBorderStyle = FormBorderStyle.None;
        frmChild.WindowState = FormWindowState.Maximized;
        frmChild.Show();
    }
    else
    {
        frmParent.Activate();
    }
  }
}
最后,我使用show form函数,如以下示例所示:

frmEmployeeManage em = null;
CommonService.ShowForm(frmEmployee, this);

我认为您需要使用
ref
参数:

  public static void ShowFrom<T>(ref T frmChild, Form frmParent) where T : Form, new()
  {
    if (frmChild == null || frmParent.IsDisposed)
    {
        frmChild = new T(); // How passing the form class here?
        frmChild.MdiParent = frmParent;
        frmChild.FormBorderStyle = FormBorderStyle.None;
        frmChild.WindowState = FormWindowState.Maximized;
        frmChild.Show();
    }
    else
    {
        frmParent.Activate();
    }
  }

ref
允许您更改方法中参数的值,这些更改也会反映在传入的变量上。

出了什么问题?非常感谢!
frmEmployeeManage em = null;
CommonService.ShowForm(ref em, this);