DialogResult that does not close the form?

I have a form Form1 from which I show Form2 as a modal form. From Form2 I do all kinds of editing and deleting various values ​​that should be reflected in Form1 after closing Form2 . So what I do is RePopulateControls_in_Form1() after closing Form2 . Since RePopulateControls_in_Form1() is a long process, I want to execute this method only if any modification (change, add, delete) occurs in Form2 , and not when Form2 just been opened and closed.

So here is what I am trying to do in Form1 :

  Form2 f = new Form2(); if (f.ShowDialog(this) == DialogResult.Something) RePopulateControls_in_Form1() 

And then in Form2 I,

  private void bntEdit() { //If Edit? this.DialogResult = DialogResult.Something; } private void bntAdd() { //If Add? this.DialogResult = DialogResult.Something; } private void bntDelete() { //If Delete? this.DialogResult = DialogResult.Something; } 

But my problem is .Something . If it's nothing but .None , Form2 just closes. I do not want Form2 just close unless the user has closed it.

If I do this:

  //in Form1 private void Form1_Click() { Form2 f = new Form2(); if (f.ShowDialog(this) == DialogResult.None) RePopulateControls_in_Form1() } //in Form2 private void Form2_SomeModification() { //If Modified? this.DialogResult = DialogResult.None; } 

RePopulateControls_in_Form1() did not hit!

In short, in my program, how can I tell the compiler to call RePopulateControls_in_Form1() only if the values ​​are changed in Form2 ?

Note. Recompilation is certainly required, because the controls are dynamically created and a bit complicated (in fact, what is created in Form2 is the controls of the graphical interface and its labels, etc.).

+6
source share
3 answers

Setting DialogResult on the form hides the form and returns from ShowDialog. If you want to postpone this until the user performs some other action (for example, closing the form), you must save this state as a member field and set DialogResult in the Form.Closing handler.

In addition, if you want to remove a modal form by clicking a button, you can use the Button.DialogResult property instead of creating a Button.Click handler.

+10
source

A simple way might be not using DialogResult at all, but a selected property that does not interfere with the behavior of the form. β€œThen you should be able to program any logic you want.”

+2
source

I would use an event in Form 2. Fire is an event when your Form2 closes. Handling this event in Form1 will allow you to do whatever processing you want. In addition, if you need, you can pass some information from Form2 to Form1 in the parameters for the event.

An alternative would be to set a global static variable - maybe just a bool. Form2 can then set it to true or false depending on whether the changes have been made. Form1 can read this when Form2 returns, and if true executes the processing and returns false.

0
source

Source: https://habr.com/ru/post/893926/


All Articles