Skip to content

Application.OpenForms encapsulation

Vigo Vigo

Asked Jun 21, 2015, 11:26 AM EDT1 answer
Share
LinkedInX
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.

Thanks

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Manoj Bhoir
    0
    Hi,
     
    Check this example. You can do like this :
     
    TypeScript
    public static class MyClass
        {
            public static void openForm(System.Windows.Forms.Form _f)
            {
                _f.Show();
            }
        }
     
    How to use :
     
    MyClass.openForm(new Form2()); 
    0