使用正确的DialogResult [英] Using DialogResult Correctly

查看:200
本文介绍了使用正确的DialogResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在回答最近的问题我有(<一个href=\"http://stackoverflow.com/questions/16808268/backgroundworker-completes-before-dowork\">Here),汉斯帕桑特说,我应该设置的DialogResult 收我的形式,而不是 form.Close的()虽然我似乎无法找出为什么?

如果我正确地读,在MSDN文档指出,这样做只会隐藏窗体,而不是正确地处理它,我相信 .Close()办?

<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.form.dialogresult.aspx\">Extract从文档。


  

Close方法当用户点击对话框的关闭按钮或设置DialogResult属性的值不会自动调用。取而代之的是,形式是隐藏的,可以再次显示而不产生对话框的新实例。由于这种行为,你必须调用形式的Dispose方法时,你的应用程序不再需要的形式。


在另一方面,微软已经创建了一个支持页面,上面写着如何使用DialogResult属性,并在验证它的工作原理的这个部分它说一下这样将关闭该窗口。

所以我的问题是双重的,我应该继续使用或关闭,而不是DialogResult的;并执行结果对话框关闭或隐藏窗体。从code下面我(一个简单的表格有两个按钮)制成,它似乎它确实隐藏仅作为 this.Close断点()是打..(与 this.Close()评论,形式还是消失了,只是不知道是否隐藏或不)

 公共Form1中()
    {
        的InitializeComponent();
        button1.Click + =(S,E)=&GT;
            {
                 //我编辑我的问题使用包括
                使用(Form1窗体=新Form1中())
                {
                    form.ShowDialog();
                }            };
        button2.Click + =(S,E)=&GT;
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            };
    }


解决方案

当您打开的ShowDialog一个模态对话框,调用code被阻塞,直到形式称为关闭或隐藏。如果你想读的所谓形式的一些公共属性和想要做的事情(例如将数据保存到数据库或文件)基础上,点击确定或取消按钮,然后你需要,如果用户想知道做动作与否。由ShowDialog的()方法返回的DialogResult的,您可以采取适当的行动...

因此​​,例如

 使用(Form1窗体=新Form1中())
{
    DialogResult的博士= form.ShowDialog();
    如果(DR == DialogResult.OK)
    {
        字符串CUSTNAME = form.CustomerName;
        的SaveToFile(CUSTNAME);
    }}

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

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?

Extract from 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.

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

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)

    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();
            };
    }

解决方案

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...

So for example

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

}

这篇关于使用正确的DialogResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆