miller
miller

Reputation: 1728

How to inform parent class about property change

I have 2 WinForms where one is parent and passing parameter to it's chilf form. The code goes something like this:

public class FormMain : Form {
private User user;

public FormMain (User user) {
InitializeComponent();
this.user = user;
}

private void btnUpdateAccount_Click(object sender, EventArgs e)
        {
            updateUser = new FormUsersUpdate(user);
            updateUser.Show();
        }
}

and this:

public class FormUsersUpdate(User user){

//Update user in database

}

User class have some usual properties like Name, surname, etc. So my question is how to inform parent class about this update without need to again retrieve user from database?

Thanks.

Upvotes: 3

Views: 937

Answers (4)

Kishore Borra
Kishore Borra

Reputation: 269

Here is the working sample for the above issue using delegate and event:

public partial class FormUsersUpdate : Form
{
    private readonly User user;
    public delegate void UserChangedEventHandler(object sender, EventArgs e);
    public event UserChangedEventHandler UsrChanged;

    private void InvokeUsrChanged()
    {
        var args = new EventArgs();
        if (UsrChanged != null) UsrChanged(this, args);
    }

    public FormUsersUpdate()
    {
        InitializeComponent();
    }

    public FormUsersUpdate(User usr)
    {
        InitializeComponent();
        this.user = usr;
        this.user.name = "Kishore";
    }

    private void FormUsersUpdate_Load(object sender, EventArgs e)
    {
        InvokeUsrChanged();
    }

}

public partial class Form1 : Form { private User user; FormUsersUpdate _frmusrUpdate;

    public Form1()
    {
        InitializeComponent();
        this.user = new User { name = "Test" };
    }


    private void button1_Click(object sender, EventArgs e)
    {
        _frmusrUpdate = new FormUsersUpdate(this.user);
        _frmusrUpdate.UsrChanged += new FormUsersUpdate.UserChangedEventHandler(_frmusrUpdate_UsrChanged);
        _frmusrUpdate.Show();
    }

    void _frmusrUpdate_UsrChanged(object sender, EventArgs e)
    {
        MessageBox.Show("User Details Changed");
    }
}

Upvotes: 0

Şafak Gür
Şafak Gür

Reputation: 7345

You can invoke a callback delegate after the update. In FormMain:

private void btnUpdateAccount_Click(object sender, EventArgs e)
{
    updateUser = new FormUsersUpdate(user, new Action<User>(OnUserUpdated));
    updateUser.Show();
}

private void OnUserUpdated(User user)
{
    // Whatever you wanted to do with the updated user.
}

In FormUsersUpdate:

public class FormUsersUpdate(User user, Action<User> callback)
{
    // Update user, then invoke the callback using the updated user instance,
    // which will call the OnUserUpdated method of the FormMain:
    callback.Invoke(user);
}

ShowDialog is mostly a better choice but I never tried it on an mdi child:

private void btnUpdateAccount_Click(object sender, EventArgs e)
{
    updateUser = new FormUsersUpdate(user);
    updateUser.ShowDialog();

    // Will wait until the user closes the dialog box.
    // FormUserUpdate keeps the updated user in a property called User:
    OnUserUpdated(updateUser.User);
}

Upvotes: 4

Maarten
Maarten

Reputation: 22945

Some options:

  • Define an event UserUpdated on your second form, and fire the event when any changes occur in the User instance.
  • Implement INotifyPropertyChanged on the User class, and handle this event in your main form when fired.

Upvotes: 3

Menefee
Menefee

Reputation: 1495

If you are simply wanting to pass the values back then expose them as public static variables in the parent and set them in the child window.

Parent:

public static User CurrentUser {get; set;}

Child:

FormMain.CurrentUser = user;    
FormMain.CurrentUser.LastName = "Menefee";

Upvotes: 0

Related Questions