打开一个表格,如果没有它的开放另一个实例 - 通过键入一个方法 [英] Open a Form if there is not another instance of it Open - Pass Type to a Method
问题描述
我想简化我的一些代码。因此,我想打一个函数至极检查,如果某种形式已经打开。现在,我有我下面的启动窗体上的每个按钮后面的代码。
<预类=郎-CSH prettyprint-覆盖>
私人无效button_parts_Click (对象发件人,EventArgs五)
{
的FormCollection FC = Application.OpenForms;
的foreach(在FC表格FRM)
{
如果(FRM是frm_parts){返回; }
}
frm_Teile newForm =新frm_parts();
newForm.Show();
}
现在我想有这样的:
私人无效button_parts_Click(对象发件人,EventArgs五)
{
StartNewForm(frm_parts);
}
私人无效StartNewForm(键入myForm)
{
的FormCollection FC = Application.OpenForms;
的foreach(在FC表格FRM)
{
如果(FRM是myForm会){返回; }
}
myForm会newForm =新建MyForm();
newForm.Show();
}
但我不能一个类型传递给一个函数
编辑:你当然可以,但我不知道如何和从哪里开始。
有(另)这样才达到我需要什么?
您可以使用这些选项之一。
使用泛型方法:
私人无效StartNewForm< T>()
,其中T:形式,新的()
{
的FormCollection FC = Application.OpenForms;
的foreach(在FC表格FRM)
{
如果(FRM是T){返回; }
}
变种newForm =新T();
newForm.Show();
}
下面是用法: StartNewForm< Form1的>( );
创建使用 Activator.CreateInstance
$ b
私人无效StartNewForm(键入myForm)
{
的FormCollection FC = Application.OpenForms;
的foreach(在FC表格FRM)
{
如果(frm.GetType()== myForm会){返回; }
}
VAR newForm =(表格)Activator.CreateInstance(myForm会);
newForm.Show();
}
下面是用法: StartNewForm(typeof运算(Form1中));
注意:
-
非通用版本在这里只是因为你想到了使用
键入
你不能做到这一点。但是你应该知道,因为它执行类型在编译时检查,而在非通用版本,你应做一些验证,例如,你应该检查是否myForm会$ C $更好的选择是使用通用版C>的类型
表格
的。 -
我没有改变你的代码,但例如你可以这样来做:
私人无效StartNewForm< T>()其中T:形式,新的()
{
变种F =(Application.OpenForms.OfType< T>()FirstOrDefault()??新T());
f.Show();
}
I want to simplify some of my code. Therefore i want to make a function wich checks, if a certain form is already open. Right now i have the code below behind every button on my start-form.
private void button_parts_Click(object sender, EventArgs e)
{
FormCollection fc = Application.OpenForms;
foreach (Form frm in fc)
{
if (frm is frm_parts) { return; }
}
frm_Teile newForm = new frm_parts();
newForm.Show();
}
Now i would like to have something like:
private void button_parts_Click(object sender, EventArgs e)
{
StartNewForm(frm_parts);
}
private void StartNewForm(Type myForm)
{
FormCollection fc = Application.OpenForms;
foreach (Form frm in fc)
{
if (frm is myForm) { return; }
}
myForm newForm = new myForm();
newForm.Show();
}
But i cannot pass a type to a function EDIT: You certainly can, but i didn't know how and where to start.
Is there a(nother) way to achive what i need?
You can use either of these options.
Using a generic method:
private void StartNewForm<T>()
where T : Form, new()
{
FormCollection fc = Application.OpenForms;
foreach (Form frm in fc)
{
if (frm is T) { return; }
}
var newForm = new T();
newForm.Show();
}
Here is the usage: StartNewForm<Form1>();
Create your form using Activator.CreateInstance
private void StartNewForm(Type myForm)
{
FormCollection fc = Application.OpenForms;
foreach (Form frm in fc)
{
if (frm.GetType() == myForm) { return; }
}
var newForm = (Form)Activator.CreateInstance(myForm);
newForm.Show();
}
Here is the usage: StartNewForm(typeof(Form1));
Note:
The non-generic version is here just because you thought you can't do it using
Type
. But you should know the better option is using generic version because it performs type checking at compile time while in the non-generic version you should perform some validations, for example you should check ifmyForm
is of type ofForm
.I didn't change your code, but for example you can do it this way:
private void StartNewForm<T>() where T : Form, new() { var f = (Application.OpenForms.OfType<T>().FirstOrDefault() ?? new T()); f.Show(); }
这篇关于打开一个表格,如果没有它的开放另一个实例 - 通过键入一个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!