Search code examples
c#winformsdialogresult

Windows Forms DialogResult


in the following code I want to get the dialog result of a Form but it's not saved to my variable... why?

My code:

public void xyz() {
    var dialogResult = new DialogResult();
    if (booleanVariable) {
        var form1 = new Form1();
        form1.ShowDialog();
        dialogResult = form1.DialogResult;
    }
    else {
        var form2 = new Form2();
        form2.ShowDialog();
        dialogResult = form2.DialogResult;
    }

    if (dialogResult == DialogResult.OK) {
        ...
    }
}

At the and of my Form1 and Form2 i set the this.DialogResult = DialogResult.OK. At the end of the process my variable dialogResult is DialogResult.None, why?


Solution

  • public void xyz() {
        var dialogResult = booleanVariable ? new Form1().ShowDialog() : new Form2().ShowDialog();
    
        if (dialogResult == DialogResult.OK) {
            ...
        }
    }