kyjan
kyjan

Reputation: 321

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?

Upvotes: 0

Views: 4497

Answers (2)

John Woo
John Woo

Reputation: 263883

try to modify this with your IF Statement:

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

Upvotes: 1

Ali Foroughi
Ali Foroughi

Reputation: 4619

public void xyz() {
    var dialogResult = booleanVariable ? new Form1().ShowDialog() : new Form2().ShowDialog();

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

Upvotes: 1

Related Questions