Windows Forms DialogResult

4.3k views Asked by At

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?

2

There are 2 answers

0
Ali Foroughi On BEST ANSWER
public void xyz() {
    var dialogResult = booleanVariable ? new Form1().ShowDialog() : new Form2().ShowDialog();

    if (dialogResult == DialogResult.OK) {
        ...
    }
}
0
John Woo On

try to modify this with your IF Statement:

DialogResult var;
Form2 qwerty  = new Form2();
var = qwerty.ShowDialog();
MessageBox.Show(var.ToString());