简体   繁体   中英

Return information from another form to the main form

I have a main form, MainForm , that opens up another form, NewPasswordForm . This happens once I click the AddButton on the main form. The code is below:

MainForm : Form {
    private void AddButton_Click(object sender, EventArgs e) {
        NewPasswordForm newPassword = new NewPasswordForm();
        using (NewPasswordForm form = new NewPasswordForm())
        {
            var result = form.ShowDialog();
            if (result == DialogResult.OK)
            {
                Console.WriteLine("Made it work");
                Login login = new Login();
                //Need to get passed the login information from the NewPasswordForm
                //so that I can add it to a list of Logins 
            }
        }            
    }
}

NewPaswordForm has four fields to fill out. Once I hit the add button in the NewPasswordForm I want

public partial class NewPasswordForm : Form {
    Login login = new Login();
    private void AddButton_Click(object sender, EventArgs e) {
        //Do stuff with the four fields to creat a Login
        //pass the login along with the DialogResult.OK below.
        this.DialogResult = DialogResult.OK;
    }

My question is how do I go about passing that information once I click the AddButton on the NewPasswordForm ?

Add a public property to your NewPasswordForm, set it's value inside the method, and then query this property from the main form.

Upd: I am taking this out of comments as requested. The question was: Why not to define and fire event along with the required data, and catch it from the main form. The answer is that the event driven architecture is meant for designing REACTIVE systems, responding to asynchronous events given by different components. But in this case the role of such an event is performed by the button pressed. There is no reason to add any asynchronous behavior besides this one. After the button is pressed, it is DETERMINED that the property is set and ready to be read. The event would be in place in case we wanted run some process after button click, and wait until it is finished.But This is not the case

Here's a code sample of the public property thing I (and Eugene Sh.) mentioned:

public partial class NewPasswordForm : Form {

    public Login LoginInfo { get; private set; }

    private void AddButton_Click(object sender, EventArgs e) {
        LoginInfo = new Login();
        //Do stuff with the four fields to create a Login
        //pass the login along with the DialogResult.OK below.
        this.DialogResult = DialogResult.OK;
    }
}

Then once you're back on the MainForm:

MainForm : Form {
    private void AddButton_Click(object sender, EventArgs e) {
        //NewPasswordForm newPassword = new NewPasswordForm(); // Don't need this line
        using (NewPasswordForm form = new NewPasswordForm())
        {
            var result = form.ShowDialog();
            if (result == DialogResult.OK)
            {
                Console.WriteLine("Made it work");
                Login login = form.LoginInfo;

                // do something
            }
        }            
    }
}

Edit: I just realized there were two NewPasswordForms instantiated in your AddButton_Click method. You don't need two for this operation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM