Pass variable from one form to another in WPF

1

I have the following problem I am trying to concatenate a variable that I get from one form to show it in another, I do it in the following way.

1.Formular

ClsIntermedia objLibreria = new ClsIntermedia();
                    if (objLibreria.Validar_Usuario(this.txtusuario.Text, this.txtcontrasena.Password.ToString(), out respuesta, out respuesta2)) 
                    {
                        Principal ventana = new Principal();
                        ventana.usuarioini = respuesta;
                        MessageBox.Show("BIENVENIDO: " +respuesta+ " "+"DE: " +respuesta2+"", "SICAP, Agrosan S.A.S", MessageBoxButton.OK, MessageBoxImage.Information);
                        ventana.Show();
                        this.Hide();
                    }
                    else
                    {
                        MessageBox.Show(objLibreria.Error, "Error del sistema", MessageBoxButton.OK, MessageBoxImage.Error);
                        objLibreria = null;
                        this.txtusuario.Text = "";
                        this.txtcontrasena.Password = "";
                        this.txtusuario.Focus();
                    }

Where the variable userini I have it published in the second form, I get the data I assign it and when I concatenate it in the second form, it does not show me the data.

2. Form

public string usuarioini ="";

            public Principal()
            {
                InitializeComponent();
                this.Title = Modelo.Util.Mensajes.MsjInicio + " " + "USUARIO: " + usuarioini + " " + "VERSIÓN: " + ConfigurationManager.AppSettings.Get("version");
            }

I do not know if it has anything to do, the fact that when the second form is launched, it loads the values that the constructor has, and since the variable takes the value, and the second form is already loaded, I do not know if it is that's why he can not carry it.

Thank you in advance for your comments.

    
asked by Brian Velez 19.12.2018 в 13:38
source

2 answers

4

The problem is exactly what you suspect. When you modify usuarioini in ventana.usuarioini = respuesta; , you have already gone through the constructor of the second form by setting the value of this.Title .

You have a very simple solution. Instead of using a variable, use a property, and in set modify this.Title . Something like this:

private string usuarioini="";
public string Usuarioini
{
    get
    {
        return this.usuarioini;
    }
    set
    {
        this.usuarioini=value;
        this.Title= Modelo.Util.Mensajes.MsjInicio + " " + "USUARIO: " + value + " " + "VERSIÓN: " + ConfigurationManager.AppSettings.Get("version");
    }

}

And in the other form, change the property:

ventana.Usuarioini = respuesta;
    
answered by 19.12.2018 / 13:52
source
0

Better work in this way Form 1

prinicipal ventandados = new prinicipal();
        AddOwnedForm(ventandados);
        ventandados.Show();

Form 2

secundario ventanauno = Owner as secundario ;
        ventanauno.usuarini = respuesta;
    
answered by 19.12.2018 в 17:42