c# problems in calling screens that even the teacher could not solve

Asked

Viewed 40 times

-1

Mens, I’m taking a DS technical course, I’m in the TCM phase and I was wondering if you can solve or help me with this problem:

Next: I am doing a CRUD in c# win Forms and I came across an error where when I call a form through a button, the same instead of opening with a click it is necessary to use two (double click to open screens). To call another form use this following method:

    private void abrirJanela(object conteudo)
    {
        if (this.panelfake.Controls.Count > 0)
            this.panelfake.Controls.RemoveAt(0);
        Form janela = conteudo as Form;
        janela.TopLevel = false;
        janela.Dock = DockStyle.Fill;
        this.panelfake.Controls.Add(janela);
        this.panelfake.Tag = janela;
        janela.Show();
        pictureBox2.Hide(); //esconde imagem do home
    }

In the method it is noticed that I describe a part to close the open screens (or at least I think this is it), only it happens that it does not close, I mean, it closes with two clicks. I believe it’s overlapping.

Below I will be leaving a short video where I will show the problem:

https://youtu.be/wWMURMs4mvY

As of now, grateful

  • Without more details there is no way to help you. The problem may be in that initial if, it may be time to open the screen, anything...

  • is kind of hard to understand, but come on... you’re wanting to redo the functionality of MDI Parent / Child, which is to open the form inside another form... ? , along with this, it would take 2 clicks to open the form, and if another form opens, the previous one should close ? Obs. Removing the form from the fake panel will not close it.

1 answer

-1

I’d try it this way:

    private void abrirJanela(object conteudo)
    {
        //programação defensiva
        if(!(conteudo is form))
        {
            MessageBox.Show("Erro no objeto recebido");
            throw new Exception();   //configurar a exception pretendida ou usar um return
        }
        if (this.panelfake.Controls.Count > 0)
            this.panelfake.Controls.RemoveAt(0);
        Form janela = conteudo as Form;
        janela.TopMost = true;
        janela.Dock = DockStyle.Fill;
        this.panelfake.Controls.Add(janela);
        this.panelfake.Tag = janela;
        pictureBox2.Hide(); //é mesmo necessário? Eu acho que talvez não...
        janela.Show();
    }

But another better way will be:

    static TipoJanela janela = null;

    private void abrirJanela()
    {
        if (this.panelfake.Controls.Count > 0)
            this.panelfake.Controls.RemoveAt(0);
        janela = new TipoJanela();   //cria um novo objeto do tipo janela ao invés de receber um. Se houver parâmetros, estes podem ser lidos de  valores estáticos ou usando o construtor
        janela.TopMost = true;
        janela.Dock = DockStyle.Fill;
        this.panelfake.Controls.Add(janela);
        this.panelfake.Tag = janela;
        pictureBox2.Hide(); //é mesmo necessário? Eu acho que talvez não...
        janela.Show();
    }

Browser other questions tagged

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