Application.OpenForms encapsulation
本文关键字:encapsulation OpenForms Application | 更新日期: 2023-09-27 17:57:21
有没有办法将这个函数封装在静态类中?
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){...}}
我想防止在所有打开的表单按钮中编写原始代码。
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();
}
}
这样称呼它:
StaticClass.Focus<frmAddWorker>(this);