Open a form from the main one and modify a principal variable without creating it again

0

using visual studio I need to modify a variable that exists in the main form from a secondary one.

To move from the main to the secondary I used the ShowDialog property to avoid closing the first because the secondary was small and I use it only to change a variable, but I have no idea how to modify that variable in the main one without creating it again.

IMG 1. Button code in the main form to access the secondary.

In the secondary constructor I pass the variable that I want to modify to put it initially as information

IMG 2. Open and secondary parent form (back) above

In the secondary form I intend to change the pre-shared key for an encryption algorithm that I use in the main form so as not to use the default one (which is not relevant), but I need that change to arrive at the main interface that is where I call the logic classes to execute the algorithm. ---- NOTE: do not look at the interfaces, it's a functional prototype

    
asked by Carlos Miguel Casas Sancesario 20.08.2018 в 19:01
source

2 answers

0

To pass variables between forms you can use 2 paths, the first, pass it by reference:

In the target form, create a public method like this:

int ClaveActual = 0;

public void MostrarFormulario(ref int clave)
{
  this.ClaveActual = clave;
  this.txtClave.Text = clave.ToString();
  this.ShowDialog();
  this.ClaveActual = int.parse(this.txtClave.Text);
}

and on the change button, enter the following code.

public void CerrarFormulario()
{
  this.Close();
}

or else to return a value, ignore the ref and stir a value:

public int MostrarFormulario(int clave)
{
  this.ClaveActual = clave;
  this.txtClave.Text = clave.ToString();
  this.ShowDialog();
  return int.parse(this.txtClave.Text);
}

and the way you call the form would be something like this:

ChangeWin objFormulario = new ChangeWin();
//Por referencia;
objFormulario.MostrarFormulario(ref Clave);
//Por valor:
int NuevaClave = objFormulario.MostrarFormulario(Clave);
    
answered by 20.08.2018 / 22:01
source
0

EDIT: I do not think it's even necessary to use events:

In your secondary form you add a property and its corresponding field:

private int newKey;
public int NewKey { return newKey;}

Then, where you have the code of the accept button:

private void BotonOk_Click(object sender, EventArgs e)
{
    newKey = // obtienes la nueva key
    DialogResult = DialogResult.OK;
    Close();
}

Finally, when you send a call to the secondary form:

var win = new Change_Key();
if (win.ShowDialog() == DialogResult.OK)
{
     var nuevaKey = win.NewKey();
     // aquí ya puedes actualizar la "key" de formulario
}

If you want the change to occur in real time before closing the second form you can use events.

You create an event like this:

public class KeyChangedEventArgs : EventArgs
{
    public KeyChangedEventArgs(int keyValue)
    {
        KeyValue = keyValue;
    }

    public int KeyValue { get;}
}

In your secondary form you add the event like this:

public class Change_Key: Form
{
    public event EventHandler<KeyChangedEventArgs> KeyChanged;

And where you change the value in that same form you send the event invoking:

var nuevaKey = // obtienes la nueva key desde donde se ha modificado
KeyChanged?.Invoke(this, new KeyChangedEventArgs(nuevaKey));

And nothing else remains to be hooked to the event from the main form:

var win = new Change_Key();
win.KeyChanged += (s , e) => {
    var nuevaKey = e.KeyValue;
    // aquí ya puedes actualizar la "key" de formulario principal a como la ocupes.
};
win.ShowDialog();
    
answered by 21.08.2018 в 06:02