There are 2 forms.

On 1 form there is a list of data and a couple of buttons, if I press the edit button, then 2 form comes out, where I enter some data and then click OK.

The question is how when you click Ok close 2 form?

  • one
    WinForm or WPF? - MaximK

2 answers 2

Open through dialogue

using (MyForm fMyForm = new MyForm()) { if (fMyForm.ShowDialog() == DialogResult.OK) { .... } } 

and in the second form, when you press a button, you call

 this.DialogResult = DialogResult.OK; this.Close(); 
  • one
    Why dr.ToString() == "OK" ? Why not dr == DialogResult.OK ? or generally fMyForm.ShowDialog() == DialogResult.OK . fMyForm.ShowDialog() == DialogResult.OK ? - kodv
  • @kodv, I agree, simply, as one of the options, it was possible through the switch - LamerXaKer
  • 2
    By the way, DialogResult can be set directly in the properties of the button and not at all writing the code for handling clicks on it - Stackoverflow.com/a/481380/177221 - PashaPash

WPF Solution
Call the second form

 MyForm form = new MyForm(); form.ShowDialog(); if (form.DialogResult == true) { ... } 

In the called form when you click "OK"

 this.DialogResult = true; Close(); 

For the Cancel button

 Close();