user8049620
user8049620

Reputation:

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?

Upvotes: 2

Views: 1775

Answers (3)

Chibueze Opata
Chibueze Opata

Reputation: 10054

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
}

Upvotes: 2

dotNET
dotNET

Reputation: 35400

Based on your description and comments, you'll need to reload your form for the colors and graphics. You can do it in one of the 3 ways:

  1. Call InitializeComponent() after you return from your settings dialog. This might be dangerous because InitializeComponent() will do other startup stuff too.
  2. Reload your main form too after returning from settings dialog. You may or may not be able to do that based on the state of your main form.
  3. Collect all the code that updates colors and graphics from InitializeComponent() and move it into a separate function. Call it after InitializeComponent() as well as when returning from your settings dialog.

I think the 3rd one would be the cleanest approach.

Edit

Another and generally much more clean way of doing this is through the use of Application Settings. You just go to your form designer, select your control and choose Application Settings from Properties window. Choose the property that you want to bind to a setting and then choose the corresponding setting from the dropdown. If the setting doesn't already exist, you just click the New button and the designer creates one for you.

These settings automatically get loaded and saved for you. No more manual stuff.

Edit 2

For immediate propagation of settings into control properties, you may need to change the default update event when binding your to your setting. To do that, go to your designer file and look for property binding statements:

this.TextBox1.DataBindings.Add("Text", Project1.Properties.Settings.Default.UserName, ""))

and set them to update immediately upon property change:

this.TextBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", global::Project1.Properties.Settings.Default, "UserName", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));

Upvotes: 0

user8049620
user8049620

Reputation:

All i need to write code on Apply_OnClick

// Get Form_Graph object
Form_Graph objGraph = (Form_Graph)Application.OpenForms["Form_Graph"];

// Create a method in Form_Graph class which apply all setting to components
objGraph.UpdateGraph();

// Now refresh Form_Graph
objGraph.Refresh();

Upvotes: 2

Related Questions