Frage

This is a very simple question yet I couldn't find a solution for it (I am not a pro programmer sorry if this is primitive!). In Form1 I have a variable called "A" and it's value is 1. I send this to Form2 and change the value to 2. And on Form2 closing I need to send the updated value to Form1. This last part I don't know how to do that and I need your help. How can I retrieve the updated value of variable A on form2 closing?

War es hilfreich?

Lösung

If you have a value that is changed by Form2, and that value is managed by Form2, you can expose it as a property of Form2, e.g.

public class Form2
{
    public string MyValue
    {
        get { return myValue; }
    }
}

and then you can retrieve it like

Form2 f2 = new Form2();
f2.ShowDialog();
string theValue = f2.MyValue;

In general you may want to check the DialogResult returned by ShowDialog() to see if the user pressed e.g. the OK or Cancel button. I'm not sure if you need that in this particular case.

UPDATE

If Form2 is not a dialog, you can instead use a callback pattern to inform Form1 that Form2 is closing to allow Form1 to retrieve any values that it needs from Form2. Alternatively you can have the callback directly supply the value you need.

Specifically, you could pass a Func<T> to Form2 that points to a callback method in Form1. Form2 would then call that Func<T> when it determines that it is closing. Here, T represents the type of variable that you want passed back to Form1.

Here's an example that assumes T is a string:

public Form2 : Form
{
    public void MyCallback(string value) { /* Do something with value */
}

public Form1 : Form
{
    Func<string> callback;
    public Form1(Func<string> callback)     
    {
        this.callback = callback;
    }

    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        if (callback != null) callback(myValue);
    }
}

Andere Tipps

WinForm has an Event called FormClosing. Right click on the form and choose properties, on right side of IDE you will get properties. There will be an icon like "lightning" in yellow color. You will find the FormClosing event there. Now add the code you want when form is closing

You could handle the the form2.FormClosing event on form1. There you can retrieve your value form2.B (provided that it is publicly accessible) on form1 as form2 is closing.

form2.FormClosing += OnFormBClosing;

private void OnFormBClosing(object sender, FormClosingEventArgs eventArgs)
{
    A = form2.B;
}

Use from closing event

private void Form2_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
   //update 
}

MSDN Library

It would probably better if you can post your actual source code.

I suppose that you open Form2 from Form1, is it right? If true, I think it would be probably better to write something like this that trying to update form1 from form2 closing event.

Form2 form2 = new Form2();
form2.A = this.A; // here this = form1
if (DialogResult.OK == form2.ShowDialog())
{
    // So here, retrieve the property from form2.
    this.A = form2.A;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top