Windows 窗体对话框结果

发布于 2024-12-25 13:27:05 字数 661 浏览 4 评论 0原文

在下面的代码中,我想获取表单的对话框结果,但它没有保存到我的变量中......为什么?

我的代码:

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) {
        ...
    }
}

在我的 Form1Form2 处,我设置了 this.DialogResult = DialogResult.OK。 在该过程结束时,我的变量 dialogResultDialogResult.None,为什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一百个冬季 2025-01-01 13:27:05
public void xyz() {
    var dialogResult = booleanVariable ? new Form1().ShowDialog() : new Form2().ShowDialog();

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

    if (dialogResult == DialogResult.OK) {
        ...
    }
}
梦初启 2025-01-01 13:27:05

尝试使用您的 IF 语句修改它:

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

try to modify this with your IF Statement:

DialogResult var;
Form2 qwerty  = new Form2();
var = qwerty.ShowDialog();
MessageBox.Show(var.ToString());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文