Interaction between forms?

13

In my program, I inserted a button and a panel . When the button is clicked, logic inserts another form in panel overlapping form initial.

No form2 that appears has a return button, which causes form2 to add up and reappear form1 , but no code I try to use is working. The button code is as follows:

//FORMULÁRIO CONTEUDO

this.panel1.Controls.Clear();
Conteudo frm = new Conteudo();
frm.TopLevel = false;

panel1.Controls.Add(frm);
frm.FormBorderStyle = FormBorderStyle.None;
panel1.Height = this.Size.Height;
panel1.Width = this.Size.Width;
frm.Size = new Size(this.panel1.Width, this.panel1.Size.Height);
Application.DoEvents();
frm.Location = new Point(0 + this.panel1.AutoScrollPosition.X, 0 + this.panel1.AutoScrollPosition.Y);
panel1.BringToFront();
panel1.Visible = true;
panel1.BackColor = Color.WhiteSmoke;
frm.Show();

And in form2 , on the button, I used the following code, and so many variants:

Form2 frm = new Form2();
frm.Controls.Clear();
this.Close();
frm.Refresh();

If you can help me, I really appreciate it. The part of opening the form is working, but the return button to form1 does not work, when I can, using the above code, form 1 is only white.

    
asked by anonymous 29.07.2015 / 23:06

4 answers

1

// To call a form simply do.

protected void btnChamaForm2_OnClick(object sender, EventArgs e)
{
  Form2 form2 = new Form2();
  form2.show();

  //e fecha o form atual assim
  this.visible = false; ou this.Clos();
}

// To return to Form1, do the same thing.

protected void btnRetornar_OnClick(object sender, EventArgs e)
{
   Form1 form1 = new Form1();
   form1.show();

   //fecha o form atual.
   this.visible = false; ou this.Clos();
}

protected void btnRetornar_OnClick(object sender, EventArgs e)
{
   Form1 form1 = new Form1();
   form1.show();

   //fecha o form atual.
   this.visible = false; ou this.Clos();
}
    
26.05.2017 / 22:15
0

So Bruno, apparently you are cleaning the controls of Form2, I believe this is what is leaving the form blank, and you do not need to instantiate the form again because when you click the button this is already the form, in the method in the controller you have to receive as parameters this:

protected void btnRetornar_OnClick(object sender, EventArgs e)
{
     this.Close();
}

If you refresh the new form installed it will appear blank again because you removed the controls from it, try to do just the basics above.

    
30.07.2015 / 14:25
0

Bruno, on the button of form1 that calls form2 you put the code:

Form2 frm = new Form2();
frm.ShowDialog();

Now on the button that closes Form2 and returns to Form1 you can by the following code.

//não há a necessidade de instanciar novamente o Form2 pois ele já foi instanciado.
this.Close(); // que ele limpa automaticamente os controles. mas pode usar o clear também se preferir.
    
08.03.2017 / 18:48
0

To open form2 do the following:

//Limpe o form atual da forma que está fazendo

//Crie uma instância de Form2
Form2 form2 = new Form2();
//Exiba o form2
form2.ShowDialog();
//Verifica se o form foi fechado
if(form2.IsDisposed)
{
    //Código para montar o form anterior
    //Acredito ser o primeiro bloco de código que você postou.
}

To get back from form2 to form1 do the following:

protected void btnRetornar_OnClick(object sender, EventArgs e)
{
    this.Close();
}
    
08.03.2017 / 19:16