Modify variables from one form to another

2

I have a problem and when I modify a tag or tag of a parent form tag from a child form, when I close the child form, the variable or tag of the parent form goes to the previous value, as if it were not I had modified.

What I do is call the constructor of the parent form from the child form, passing the parameter and this parameter assigning it to a tag of a tag of the parent form.

Child form

Form2 frm2;
frm2 = new Form2("false");

Parent form

public Form2(string mostradoMenu)
{
    menu.Tag = mostradoMenu;
}

PS: menu is a label of the parent form

I do not find anything related to that and I can not solve it. I have tried with setters, getters and others, and nothing.

If anyone knows, it would be very helpful. Thanks in advance.

    
asked by Adro Cruz 12.07.2018 в 11:08
source

2 answers

2

Your problem is very common among beginners in programming, and is based on the differentiation between a class and an instance.

Let's put this example. Let's create two Form2 :

Form2 frm1= new Form2();
Form2 frm2= new Form2();

Here what we have are two different instances of Form2 . If we do the following:

frm1.Tag="prueba";

The only affected instance is frm1 . If we consult frm2.Tag it will be null , since although the class used to create the object is the same Form2 , the objects are different.

Applied to your case, when in your child form you do Form2 frm2; frm2 = new Form2("false"); you are creating a new instance of Form2 , and the tag will not be modified in the existing parent form, but in this new object, which will disappear at the time the child form disappears.

A very simple solution is to create in the child form a constructor that receives the instance of the father, something like:

Form2 formularioPadre;

public FormularioHijo(Form2 formulario)
{
    this.formularioPadre=formulario;
}

And from the parent form, when creating the child, we would use this new constructor:

FormularioHijo frmHijo= new FormularioHijo(this);
frmHijo.Show();

This way, in the child form in formularioPadre we would already have a link to the instance of the parent form.

To modify the tag then, you simply have to do the following:

this.formularioPadre.Tag = false;
    
answered by 12.07.2018 / 12:59
source
1

To pass data between the forms I do it by methods;

If you open the form by clicking on a button (for example) you can pass data to the new form in this way;

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        f2.recuperarValores("Nuevo valor");
        f2.Show();
    }

And the form 2 will recover it;

    public void recuperarValores(String valorRecuperado)
    {
        this.label1.Text = valorRecuperado;
        this.label1.Tag = valorRecuperado;
    }

In the case you mention, you want to do it when closing it.

You will have to make sure that you do not create a new instance, since in this way you will be resetting the values;

Form1

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2(this);
        f2.recuperarValores("Nuevo valor");
        f2.Show();
    }

    public void RecuperarValorAlCerrar(string valor)
    {
        this.button1.Text = valor;
        this.button1.Tag = valor;
    }

Form2

public partial class Form2 : Form
{
    Form1 form ;
    public Form2(Form1 f)
    {
        InitializeComponent();
        form = f;
    }

    private void Form2_Load(object sender, EventArgs e)
    {

    }

    public void recuperarValores(String valorRecuperado)
    {
        this.label1.Text = valorRecuperado;
        this.label1.Tag = valorRecuperado;
    }

    private void btnCerrar_Click(object sender, EventArgs e)
    {

        form.RecuperarValorAlCerrar("Valor2");
        this.Close();
    }
}

This way you will be saving in the child form the instance of the one that invoked it and you will assign the value without losing it.

    
answered by 12.07.2018 в 12:46