C# Application.OpenForms封装
有没有办法将这个函数封装在一个静态类中C# Application.OpenForms封装,c#,winforms,C#,Winforms,有没有办法将这个函数封装在一个静态类中 private void btnAddWorker_ItemClick(object sender, ItemClickEventArgs e) { bool isOpen = false; foreach (Form _f in Application.OpenForms) { if (_f is frmAddWorker) { isOpen = true;
private void btnAddWorker_ItemClick(object sender, ItemClickEventArgs e)
{
bool isOpen = false;
foreach (Form _f in Application.OpenForms)
{
if (_f is frmAddWorker)
{
isOpen = true;
_f.Focus();
break;
}
}
if (isOpen == false)
{
frmAddWorker AddWorker = new frmAddWorker() { MdiParent = this };
AddWorker.Show();
}
}
诸如此类:
public class Forms(){public void openForm(form _f){...}}
我想防止在所有打开的表单按钮中写入原始代码。公共静态无效焦点(表单父级),其中T:System.Windows.forms.Form
public static void Focus<T>(Form parent) where T : System.Windows.Forms.Form
{
bool isOpen = false;
foreach (var f in Application.OpenForms)
{
if (f is T)
{
isOpen = true;
(f as Form).Focus();
break;
}
}
if (!isOpen)
{
T newForm = Activator.CreateInstance<T>();
newForm.MdiParent = parent;
newForm.Show();
}
}
{
bool-isOpen=false;
foreach(Application.OpenForms中的var f)
{
if(f是T)
{
isOpen=真;
(f作为形式)。焦点();
打破
}
}
如果(!isOpen)
{
T newForm=Activator.CreateInstance();
newForm.MdiParent=父项;
newForm.Show();
}
}
可以这样称呼:
StaticClass.Focus<frmAddWorker>(this);
StaticClass.Focus(这个);
将t
约束为一个表单不是很明智吗?而且。首先
不起作用,因为它没有实现IEnumerable
。就像我说的“未测试”