创建显示表单的全局方法(Create global method to show Forms) Form1 frm = new Form1(); frm.mdiParent=this; frm.show();

我想创建一个全局方法showFrm(Form formToShow) ,我可以从我项目的任何地方访问它。

我试过了。

public void showFrm(Form formToShow) { formToShow f=new formToShow();//getting error here f.mdiParent= mdiForm; f.show(); } Form1 frm = new Form1(); frm.mdiParent=this; frm.show();

I want to create a global method showFrm(Form formToShow) which i can access from anywhere in my project.

I've tried.

public void showFrm(Form formToShow) { formToShow f=new formToShow();//getting error here f.mdiParent= mdiForm; f.show(); }

最满意答案

将此方法放在“仅父表单”类中:

public void ShowMdiChild<T>() where T: Form, new() { var form = new T(); form.MdiParent = this; form.Show(); }

用法:

yourOnlyParentForm.ShowMdiChild<SomeForm>();

我还提醒您,C#区分大小写。

Put this method in your "only parent form" class:

public void ShowMdiChild<T>() where T: Form, new() { var form = new T(); form.MdiParent = this; form.Show(); }

Usage:

yourOnlyParentForm.ShowMdiChild<SomeForm>();

I would also remind you that C# is case-sensitive.

更多推荐