Interaction between forms?

Asked

Viewed 590 times

13

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

In the form2 that appears has a return button, which causes the form2 disappear and reappear the 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 the form2, on the button, I used the following code, and other so many variants:

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

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

  • You said you’re using [tag:webforms], correct? This one Form refers to the <form runat="server">? Or something different that you created?

  • Sorry the error @brazilianldsjaguar , I am using windowsforms (visual studio). It seems that the question has been edited.

  • I could post a print of the two forms, and how I would like transitions to occur.

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();
}

0

So Bruno, apparently you’re cleaning the Form2 Controls, I believe that’s what’s leaving the form blank, and you don’t 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 instated form it will appear blank again because you have removed the Controls from it, try to do only the basics above.

  • It remained white the background, however, my program is working as a "tree", there is the initial form, and to keep its menu on the sides, I inserted the form2 inside the control of the initial form, thus, when clicking a button opens the form2 inside the Form1, as two in one, when it is clicked a certain button in the form2 it opens a third form, in which it contains the return button, in which I cannot make it return to the form2, I imagine that there is a conflict of controls between the Formulars. Thanks for the help @Thiago Falcão

0

Bruno, on the Form1 button that calls form2 you enter the code:

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

On the button that closes Form2 and returns to Form1 you can put 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.

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 return from form2 to Form1 do the following:

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

Browser other questions tagged

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