Reputation: 2180
I know there are many questions about this one, and I saw most of them but still non of them seems work for me. I have a parent and a child forms. Child form has two buttons - save and cancel, and I want when one of them is pressed to refresh the parent form(basically to get the new data).
I've tried passing the parent form as a parameter when calling .showDialog()
and then in the child function calling parentForm.Refresh()
and I can see that it's called but the form doesn't refreshes..
I've also tried adding this.Refresh()
in the parent form after the childForm.showDialog()
line with the hope that after the child form closes, the refresh will called.. doesn't work.
I've tried this without any luck as well
if (ftpsf.DialogResult == DialogResult.OK) // what button should I call when I don't OK?
{
this.Refresh();
}
And I've also tried subscribing to the close event of the child form, no success..
private void frm2_FormClosed(object sender, FormClosedEventArgs e)
{
this.Refresh();
}
Does anybody have any other suggestions?
Upvotes: 0
Views: 5635
Reputation: 1508
Maybe a stupid remark, but if you want to start over. Why don't you use:
Application.Restart()
Upvotes: 1
Reputation: 12809
Note that the basic implementation of the Form.Refresh
method usually just redraws the form, it doesn!t update your contols' state. Implement for example a RefreshData
method for yourself for this.
Anyway, I recommend you to use a slightly different approach. I used to solve such scenarios with global UI events, so that any form can react on data changes, doesn't matter where from it came).
For example you can define an event like this:
public static class DataEvents {
public static event EventHandler DataChanged;
internal static void RaiseDataChanged() {
var handler = DataChanged;
if (handler != null)
handler();
}
}
And then listen to this event in your parent form, and raise it in your child form, or whereever you think you need to indicate a data change which should be reflected. This way your parent and child forms are more loosly coupled, which is usually a better design.
In form constructor for example:
...
DataEvents.DataChanged += (s,e) => RefreshData();
...
And when you indicate a change:
if (DialogResult == DialogResult.OK)
{
...
DataEvents.RaiseDataChanged();
...
}
And your RefreshData method could be something like this:
private void RefreshData()
{
myGridView.Reload(); // just an example
}
Upvotes: 2