C# - How to open Form2 without losing the Textbox information that is in Form1 and vice versa?

Asked

Viewed 78 times

0

namespace TesteAbertura
{
    public partial class FormularioTeste : Form
    {
        public FormularioTeste()
        {
            InitializeComponent();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {

            panel1.Controls.Clear();
            Form1 f = new Form1();          
            f.TopLevel = false;
            panel1.Controls.Add(f);
            f.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            panel1.Controls.Clear();
            Form2 f = new Form2();
            f.TopLevel = false;
            panel1.Controls.Add(f);
            f.Show();

        }
  • I don’t understand ...?

  • In each of the forms has a text box that I type something, when I click on the button to change to Form2 what was typed in the Form1 text box disappears, I want to remain what was typed in the text boxes of both forms.

  • Don’t just take that panel1.Controls.Clear()?

  • Linq take Panel1.Controls.Clear() does not solve.

2 answers

0

You can create a variable and receive this data in Form 2, and when you return you fill again.

For example :

public Form2(string text) { //....... }

And when you return to the other form you return what was written.

  • David Pereira thanks for your reply, I want to tell you that I am new in programming and study by hobby, I ask for kindness could make your answer even clearer so that I can understand better and manage to include your example in the code.

0


By calling:

Form1 f = new Form1();

You are creating a new Form1 object, if every time you click the button, you create a new one, it will surely lose the information in the textbox.

You can put the Forms setting in the main form, and show them when necessary, thus retaining the information in the controls:

namespace TesteAbertura
{
    public partial class FormularioTeste : Form
    {
        // Definição no escopo da classe
        Form1 f1 = new Form1();
        Form2 f2 = new Form2(); 

        public FormularioTeste()
        {
            InitializeComponent();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            panel1.Controls.Clear();
            f1.TopLevel = false;
            panel1.Controls.Add(f1);
            f1.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            panel1.Controls.Clear();
            f2.TopLevel = false;
            panel1.Controls.Add(f2);
            f2.Show();
        }
}

However, if you close the Form, the GC will collect it anyway (the form will be dropped), you will need to cancel the closing event:

private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
    e.Cancel = true;
    Hide();
}

Normally you do not use Form inside a Panel, Forms are abstractions of windows, but nothing prevents you (I was amazed that it works), the most suitable for your case is to use Usercontrol or even two panels alternating their visibility.

  • Edney thank you so much for solving my problem, it worked perfectly, now the information doesn’t disappear anymore. As I said above to David Pereira I am new to programming and I always try to learn and understand the C#language. May you and everyone who’s tried to solve this problem have a great week.

Browser other questions tagged

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