Is it possible to create a class from a Forms opening routine? How?

Asked

Viewed 70 times

0

I have several forms that I’m 'instantiating' by the event click on the toolstrip, but it seems that it is getting polluted since all codes do the same routine changing only the parameters.

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

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 is no longer open the Registration screen - Suppliers:

 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 canvases as well.

How could, if possible simplify this repeated routine?

  • It is possible yes using Generics, but is all this necessary? Something tells me that there is a better solution.

  • @Maniero This way is attending me so far.. do not know if it is the best or worst solution, about the Generics that you send, I do not understand how I will adapt the code, there you used console etc

  • Meet and be right not different thing.

1 answer

4


It is possible using Generics. You create a generic method that receives a Form, the method knows it is a Form, then you can manipulate him. The code becomes 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();
    }
}

Utilizing:

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

Explanation:

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

I search in the list of open Forms MdiChildren an instance of the generic form type T (OfType<T>), depending on who you’re calling, T can be a frmCadastroPessoa, frmCadastroFornecedor, frmCadastroCachorro, etc....

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

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

else
{
    form.Activate();
}

If he was found, that means he already exists, then just activate him.

Reference

  • It would be possible for you to explain to me what makes the var line exactly the part of . Firstordefault() and there in the if . isDisposed?

  • 1

    Ready, at the end of the answer I put details of the code.

  • Thanks bro, gave it right to what it was needing here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.