Is there any way to encapsulate this function in a static class?
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();
}
}
something like that:
public class Forms(){public void openForm(form _f){...}}
I want to prevent write the original code in all forms buttons that open.
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();
}
}
Call it like this:
StaticClass.Focus<frmAddWorker>(this);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.