0

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?

Apostrofix
  • 2,140
  • 8
  • 44
  • 71
  • 2
    Why do you need to do this? There aren't many times in Windows Forms when you need to refresh. You might well, I don't mean to tread on your knowledge here or anything, just are you sure that's the actual functionality you're looking for? – Matthew Haugen Jul 14 '14 at 08:02
  • Have you overriden the `Refresh` method to do something to actually get the updated data? – Rowland Shaw Jul 14 '14 at 08:03
  • 2
    What do you expect `Refresh` method to do? – Sriram Sakthivel Jul 14 '14 at 08:05
  • 1
    `Refresh` is a graphical thing, it has nothing to do with data values, you need to look into either databinding, or making a custom method you can call that will update values. As a warning, if you update one value in a data bound class, it updates all the values in the data bound class; this is a [winforms issue](http://stackoverflow.com/questions/16980942/update-databinding-on-lost-focus-winforms) – Sayse Jul 14 '14 at 08:10

2 Answers2

2

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
}
Zoltán Tamási
  • 12,249
  • 8
  • 65
  • 93
  • That's a nice way to accomplish it. Thank you for the suggestion. In my case I just had to update a comboBox, so I decided that it's an overkill to refresh(that is actually not the same as what I thought) the whole form. – Apostrofix Jul 14 '14 at 08:52
  • 1
    @Ivo Many times in my past experiences when I thought something is an overkill, I had regret it later on. :) I understand your point though, it really depends on overall application complexity. – Zoltán Tamási Jul 14 '14 at 08:58
1

Maybe a stupid remark, but if you want to start over. Why don't you use:

Application.Restart()
Revils
  • 1,478
  • 1
  • 14
  • 31