C# 正确使用 DialogResult

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16846573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 07:56:16  来源:igfitidea点击:

Using DialogResult Correctly

c#winformsdialogresult

提问by Sayse

In an answer to a recent question I had (Here), Hans Passant stated that I should set the DialogResultto close my forms instead of form.Close()although I cannot seem to find out why?

在对我最近提出的一个问题的回答中(此处),Hans Passant 表示我应该将 设置DialogResult为关闭我的表单,而不是form.Close()尽管我似乎无法找出原因?

If I've read correctly, the MSDN documentation states that doing this will just hide the form instead of correctly disposing it which I believed .Close()to do?

如果我读对了,MSDN 文档指出,这样做只会隐藏表单,而不是像我认为.Close()的那样正确处理它?

Extractfrom documentation.

从文档中提取

The Close method is not automatically called when the user clicks the Close button of a dialog box or sets the value of the DialogResult property. Instead, the form is hidden and can be shown again without creating a new instance of the dialog box. Because of this behavior, you must call the Dispose method of the form when the form is no longer needed by your application.

当用户单击对话框的关闭按钮或设置 DialogResult 属性的值时,不会自动调用 Close 方法。相反,窗体被隐藏并且可以再次显示而无需创建对话框的新实例。由于这种行为,当您的应用程序不再需要表单时,您必须调用该表单的 Dispose 方法。

On the other hand, Microsoft has created a support pagethat says how to use DialogResult property and in the "Verify It Works" section of this it states that clicking so will Close the form.

另一方面,Microsoft 已经创建了一个支持页面,说明如何使用 DialogResult 属性,并在此的“验证它有效”部分中声明单击“如此”将关闭表单。

So my question is two fold, should I continue to use Close or DialogResult instead; and does dialog result close or hide a form. From the code I made below (a simple form with two buttons), it would seem that it is indeed hidden only as a breakpoint on this.Close()is hit..(with this.Close()commented, the form still disappears, just not sure whether hidden or not)

所以我的问题是双重的,我应该继续使用 Close 或 DialogResult 吗?并关闭或隐藏表单对话框结果。从我在下面制作的代码(一个带有两个按钮的简单表单)来看,它似乎确实只有在this.Close()击中断点时才隐藏..(有this.Close()评论,表单仍然消失,只是不确定是否隐藏)

    public Form1()
    {
        InitializeComponent();
        button1.Click += (s, e) =>
            {
                 //I edited my question to include using
                using(Form1 form = new Form1())
                {
                    form.ShowDialog();
                }

            };
        button2.Click += (s, e) => 
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            };
    }

采纳答案by Steve

When you open a modal dialog with ShowDialog, the calling code is blocked until the form called closes or hides. If you want to read some public properties of the called form and want to do things (for example save data to a database or to a file) based on the click on the OK or Cancel button, then you need to know if the user wants to do the action or not. The DialogResult returned by the ShowDialog() method allows you to take the appropriate actions...

当您使用 ShowDialog 打开模式对话框时,调用代码将被阻止,直到被调用的窗体关闭或隐藏。如果您想读取被调用表单的一些公共属性并希望基于单击“确定”或“取消”按钮执行某些操作(例如将数据保存到数据库或文件),那么您需要知道用户是否想要做与不做的动作。ShowDialog() 方法返回的 DialogResult 允许您采取适当的操作...

So for example

所以例如

using (Form1 form = new Form1())
{
    DialogResult dr = form.ShowDialog();
    if(dr == DialogResult.OK)
    {
        string custName = form.CustomerName;
        SaveToFile(custName);
    }

}

回答by Dan

Whether you call Closeor set the DialogResultproperty is not really the issue. You just need to make sure to call Dispose. I prefer doing this with a using block:

您是否调用Close或设置DialogResult属性并不是真正的问题。您只需要确保调用Dispose. 我更喜欢使用 using 块执行此操作:

using (Form1 form = new Form1())
{
    form.ShowDialog();
}

I originally thought that you could call ShowDialogon a Formthat has already had its Closemethod called. This is not the case. If you show the form modally with ShowDialog, it doesn't seem to matter whether it is closed as a result of the Closemethod, or setting the DialogResultproperty. It would seem that setting the DialogResultis just a short-cut for closing the Form.

我最初认为你可以调用ShowDialog一个Form已经调用了它的Close方法。不是这种情况。如果您使用 以模态方式显示表单ShowDialog,则它是否因Close方法或设置DialogResult属性而关闭似乎无关紧要。似乎设置DialogResult只是关闭Form.

But whether you call Closeor set the DialogResultproperty, the key is to make sure that you call Dispose()or put your form in a using block.

但是,无论是调用Close还是设置DialogResult属性,关键是要确保调用Dispose()或将表单放在 using 块中。