Is it possible to create a class from a routine to open forms? As?

0

I have several forms that I 'instantiate' by the click event in the toolstrip, however it seems that it is getting polluted since all codes do the same routine changing only the parameters.

Event to open and check if the Registration - Clients screen is no longer open:

private void cadastrarToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            //Formulário de Cadstro de Clientes
            //Verifica se já existe o mesmo formulário aberto para não sobrecarregar de processos iguais

            bool aberto = false;

            foreach (frmCadastroClientes f in this.MdiChildren.OfType<frmCadastroClientes>()) //OfType - Filtro que retorna só o que especifiquei
                if (f.Name == "frmCadastroClientes")
                {
                    f.Activate();
                    aberto = true;
                    break;
                }

            if (!aberto)
            {

                frmCadastroClientes CadastroClientes = new frmCadastroClientes();
                CadastroClientes.StartPosition = FormStartPosition.CenterParent;
                CadastroClientes.MdiParent = this;
                CadastroClientes.Dock = DockStyle.Fill;
                CadastroClientes.Show();
            }
        }

Event to open and check if the Registration - Suppliers screen is no longer open:

 private void cadastrarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Formulário de Cadastro de Fornecedores
            //Verifica se já existe o mesmo formulário aberto para não sobrecarregar de processos iguais

            bool aberto = false;

            foreach (frmCadastroFornecedores f in this.MdiChildren.OfType<frmCadastroFornecedores>()) //OfType - Filtro que retorna só o que especifiquei
                if (f.Name == "frmCadastroFornecedores")
                {
                    f.Activate();
                    aberto = true;
                    break;
                }

            if (!aberto)
            {

                frmCadastroFornecedores CadastroFornecedores = new frmCadastroFornecedores();
                CadastroFornecedores.StartPosition = FormStartPosition.CenterParent;
                CadastroFornecedores.MdiParent = this;
                CadastroFornecedores.Dock = DockStyle.Fill;
                CadastroFornecedores.Show();
            }
        }

And so it goes .. on other screens too.

How could, if possible, simplify this repeated routine?

    
asked by anonymous 26.09.2017 / 14:10

1 answer

4

It is possible using Generics . You create a generic method that gets a Form , the method knows that it's a Form , so you can handle it. The code is much simpler.

Generic Method:

public void CreateMdiChildOrActivate<T>() where T : Form, new()
{
    var form = this.MdiChildren.OfType<T>().FirstOrDefault();
    if (form == null || form.IsDisposed)
    {
        form = new T();
        form.StartPosition = FormStartPosition.CenterParent;
        form.MdiParent = this;
        form.Dock = DockStyle.Fill;
        form.Show();
    }
    else
    {
        form.Activate();
    }
}

Usage:

private void cadastrarToolStripMenuItem1_Click(object sender, EventArgs e)
{
   CreateMdiChildOrActivate<frmCadastroClientes>();
}

Explanation:

var form = this.MdiChildren.OfType<T>().FirstOrDefault();

I look for in the list of open forms MdiChildren an instance of form generic type T ( OfType<T> ), depending on who is calling, T can be frmCadastroPessoa , frmCadastroFornecedor , frmCadastroCachorro , etc ...

if (form == null || form.IsDisposed)

If the form is not found ( null ) or has been deleted ( disposed ), I create a new one ( new T() ).

else
{
    form.Activate();
}

If it has been found, it means that it already exists, so just activate it.

Reference

    
26.09.2017 / 14:27