Xander Cage Xander Cage - 1 year ago 43
C# Question

Update form when other form is active

I have two Forms ( Form_Graph(Main Form), Form_Setting) and one Setting file. When I click on the Setting button, Form_Setting is opened using ShowDialog().

Form_Setting Contains three buttons OK, Cancel, Apply and setting option. Now the problem is when I change Setting and update setting file and after Click on Apply button, I'm not able to apply this setting to Form_Graph.
(Apply_OnClick saves the new setting in setting files.)

I have tried to refresh Form_Graph using:


  1. Form_Graph obj = new Form_Graph();

  2. Application.OpenForms["Form_Graph"].Refresh();



And also I have debugged it. All Form_Graph code is executing on both the way but hasn't applied the settings.

I know the first way never works because I created one new Form, but what about the second one?

Can anyone have a solution for this?

Answer Source

You don't need to create a new instance of parent in child. The best way to do this normally is to subscribe to events from the child form i.e. Form_Setting. You will need to create an event in the child form as follows:

public event EventHandler SettingsApplied;

public void NotifySettingsApplied(EventArgs e)
{
    if(SettingsApplied != null)
        SettingsApplied(this, e);
}

public void Apply_OnClick(object sender, EventArgs e)
{
    //trigger event here to notify main form
    NotifySettingsApplied(e);
}

Then in your parent form, subscribe to this event in the constructor or any other suitable place:

public Form_Graph()
{
     fs = new Form_Setting();
     fs.SettingsApplied += new EventHandler(fs_SettingsApplied);
}

void fs_SettingsApplied(object sender, EventArgs e)
{
     //update your main graph form here
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download