2

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?

  • 1
    Possible duplicate of this one: https://stackoverflow.com/questions/30532178/refresh-an-open-form-from-another-open-form – SWalters May 22 '17 at 18:20
  • winforms or wpf, can't be both. Looking at your code snippets it looks like winforms to me. – dotNET May 22 '17 at 18:45
  • What settings does your main form depend upon? How/when does it read those settings? – dotNET May 22 '17 at 18:47
  • in setting file there are colors, And some boolean properties. in form_graph there are some button and some graphic. So when InitializeComonent () called i have given setting values to this button and graphic so it will update when setting change @dotNET –  May 22 '17 at 19:22

3 Answers3

2

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();
2

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
}
Chibueze Opata
  • 9,856
  • 7
  • 42
  • 65
0

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));
dotNET
  • 33,414
  • 24
  • 162
  • 251
  • i have try with 1 and 3 but still not able to apply settings. And when i click on apply button the Form_Setting is not closing anymore it have to focused and open so i can not apply 2nd way . Any change in setting will need to apply on Form_Graph @dotNET –  May 22 '17 at 19:36
  • I think it's important to add https://stackoverflow.com/questions/33348930/editing-the-initializecomponent-method-c-sharp and other links talking about InitializeComponent. Generally, because of issues just like this, it's a bad idea to modify the code in InitializeComponent; it should all be code generated by the Designer. Note that manually changing this function will change what appears in the designer. Generally, if you have added any changes into InitializeComponent, it's better to put those in a separate function that you call after the form is created (but before it's opened). – Scott Mermelstein May 22 '17 at 19:37
  • The really correct way of doing this is by using the **Application Settings** feature available in winforms. You can bind your controls' properties directly to your settings and they get loaded automatically upon startup (and saved when you exit your form). I don't know if you have enough skill to follow that stuff, but just in case you're interested, see my edit. – dotNET May 22 '17 at 19:42
  • exactly i have done that(Here setting file is Properties->Settings->Defaults) but problem is it updated automatically during startup but didn't update when program is running and have change in setting files @dotNET –  May 23 '17 at 03:39
  • @XanderCage: See Edit 2. – dotNET May 23 '17 at 06:44
  • But problem is i cannot access TextBox1 from Setting Form so how can i update it? i Need a method which will do this @dotNET –  May 23 '17 at 07:19
  • Your solution will work perfect when i create all component to public and then **Form_Graph objGraph =(Form_Graph)Application.OpenForms["Form_Graph"];** . **objGraph.TextBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", global::Project1.Properties.Settings.Default, "UserName", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));** –  May 23 '17 at 07:38
  • @XanderCage: You don't need to access `TextBox1` from settings form. You just assign a value to your setting like `Project1.Properties.Settings.Default.UserName = "SomeValue";` and then call `Project1.Properties.Settings.Default.Save();` when closing the settings dialog. Your main form will automatically read the new values. – dotNET May 23 '17 at 07:41
  • Settings will update but not apply till i refresh Form_Graph because i am still in Setting Form @dotNET –  May 23 '17 at 07:51