Hello. There is a main window in which the user works, and if necessary to change any data, he presses the edit button and a new window is created. In it, I transmit information from the main without any problems. In the new window, the user edits the data and there are 2 buttons on it, one closes the window, and the second one - “Save”. that the child form "refuses" to work with the main one. How to fix it?

Thank you, @Konstantin25 . Here's something that came up, but it seems that is too abstruse ...

---- ``-Создаю статический класс public static class Values { public static string V1; public static string V2; //........ } 

On the button to display the second form in the main form I write

 private void button2_Click(object sender, RoutedEventArgs e) { Form_add = new Form_Add(); //Здесь передаю значения из главной в дочернюю и пишу это... Form_add.Dobavit+=Dobavlenie; Form_add.ShowDialog(); } void Dobavlenie(object sender,EventArgs e) { label1.Content = Values.V1;//В этой метке,например на главной форме я //получу значения из статического класса, // а в него внесу значения из дочерней формы } 

In the child window, when I press the button, I write

  private void button_dobavit_(object sender, RoutedEventArgs e) { Values.V1= this.DatePicker1.SelectedValue.ToString(); Dobavit(sender, e); this.Close(); } public event EventHandler Dobavit = delegate { }; 
  • He says - I will not and everything? :-) more specifically you can? - Chad
  • She says, “Give me money! Just because I won’t work” :) I can’t see the main form while in the subsidiary. (I read the message of Constantine25 there are many examples, it should turn out) - Rakzin Roman

2 answers 2

Read here and understand everything: how to transfer the data from one form to another?

    If you have a precisely defined child relationship - the main form, then you can pass the reference to the main form in the constructor of the child.

    Further, it is trivial, either make the variable properties of the main form as a property with public setters, or encapsulate the logic for changing these properties into methods like ChangeXYZSettings.


    In any case, by the way, this is a bad design, since the child form usually does not need to know anything concrete about the main form and it’s worth changing the data to the model, from where they will be pulled in a known way.

    • For what minus set? - Asen2
    • It's not me. There is a lot of information ... I rework, while I only came up with a perverted version - Rakzin Roman