Hello. I want to know the reason for closing the next window.

enter image description here

The fact is that CloseReason.UserClosing is set to true both when you close the cross, and when you click the Accept button. And I need to know whether the cross was pressed specifically. How can this be implemented?

Button processing code:

 private void button1_Click(object sender, EventArgs e) { fileName = textBox1.Text; this.Close(); } 

    1 answer 1

    Simple example with DialogResult

     public class MyForm : Form { public MyForm() { var bt = new Button() {Text = "Apply"}; this.Controls.Add(bt); bt.Click += (sender, args) => { this.DialogResult = DialogResult.Yes; // также тут можно вернуть OK, Abort, и так далее this.Close(); }; } } 

    how to check

     var form = new MyForm(); var ret = form.ShowDialog(); Console.WriteLine(ret); 

    Clicking on the cross will return Cancel, when you click on the button - Yes