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);