是否有可能超载的形式ShowDialog方法,并返回一个不同的结果呢? [英] Is it possible to overload the ShowDialog method for forms and return a different result?

查看:203
本文介绍了是否有可能超载的形式ShowDialog方法,并返回一个不同的结果呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:这种方法实际上的伟大工程,我问它,然后找到解决方案后。我加在重载的ShowDialog()调用正确的方法(这不是exacly过载,甚至是超越,但它的工作原理是一样的。我的新问题是,一个在底部。

我在你点击有三种按钮的形式我已经定义了一个枚举对返回的结果我想拨打电话:

I have a form in which you click one of three buttons. I have defined an enum for the returned results. I want to make the call:

MyFormResults res = MyForm.ShowDialog();

我可以添加一个新的ShowDialog方法与此代码:

I can add a new ShowDialog method with this code:

public new MyFormResults ShowDialog()
{
    //Show modal dialog
    base.ShowDialog(); //This works and somehow I missed this

    return  myResult; //Form level variable (read on)
}

我设定的结果窗体级变量被点击的按钮后:

I set a form-level variable for the result when the buttons are clicked:

MyFormResults myResult;

private void btn1_click(object sender, EventArgs e)
{
    myResult = MyFormsResults.Result1;
    this.DialogResult = DialogResult.OK; //Do I need this for the original ShowDialog() call?
    this.Close(); //Should I close the dialog here or in my new ShowDialog() function?
}

//Same as above for the other results

我唯一缺少的是显示对话框(模态),然后返回我的结果的代码。没有 base.ShowDialog()的功能,让我怎么做到这一点。

The only thing I'm missing is the code to show the dialog (modal) and then return my result. There is no base.ShowDialog() function, so how do I do this?

修改?有一个'base.ShowDialog()'和它的作品。这这里是我的新的问题:

此外,这是要做到这一切,为什么最好的办法

Also, is this the best way to do all this and Why?

感谢

推荐答案

编辑:这proberly不改变的ShowDialog(),人们希望它返回一个的DialogResult 并展示形式,我建议像我下面的建议。因此,仍允许的ShowDialog()要使用的正常方式。

It's proberly not a good idea to change the functionality of ShowDialog(), people expect it to return a DialogResult and show the form, I suggest something like my suggestion below. Thus, still allowing ShowDialog() to be used the normal manner.

您可以在创建一个静态方法您 MyForm的,像 DoShowGetResult()

You could create a static method on your MyForm, something like DoShowGetResult()

这将像这样

public static MyResultsForm DoShowGetResult()
{
   var f = new MyForm();
   if(f.ShowDialog() == DialogResult.OK)
   {
      return f.Result;   // public property on your form of the result selected
   }
   return null;
}



然后你可以使用这个

then you can use this

MyFormsResult result = MyForm.DoShowGetResult();

这篇关于是否有可能超载的形式ShowDialog方法,并返回一个不同的结果呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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