是否可以重载表单的 ShowDialog 方法并返回不同的结果?

发布于 2024-09-24 05:57:39 字数 1058 浏览 10 评论 0原文

编辑:这个方法实际上效果很好,我问了它然后找到了解决方案。我在重载的 ShowDialog() 方法中添加了正确的调用(这不完全是重载,甚至不是覆盖,但它的工作原理是一样的。我的新问题是底部的问题。

我有一个表单您单击三个按钮之一,我已经为返回的结果定义了一个枚举,我想进行调用:

MyFormResults res = MyForm.ShowDialog();

我可以使用以下代码添加一个新的 ShowDialog 方法:

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

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

当按钮打开时,我为结果设置一个表单级变量。 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() 函数,那么我该怎么做

? b>编辑:有一个“base.ShowDialog()”,它可以工作。这是我的新问题:

另外,这是执行所有这些操作的最佳方法吗?

为什么?

EDIT: This method actually works great and I asked it then found the solution later. I added the correct call in the overloaded ShowDialog() method (it's not exacly an overload, or even an override, but it works just the same. My new question is the one at the bottom.

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

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

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?

EDIT: There is a 'base.ShowDialog()' and it works. This is my new question here:

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

Thanks.

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

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

发布评论

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

评论(4

荆棘i 2024-10-01 05:57:40

编辑:改变 ShowDialog() 的功能可能不是一个好主意,人们期望它返回 DialogResult 并显示表单,我建议像下面的建议一样。因此,仍然允许以正常方式使用 ShowDialog()

您可以在 MyForm 上创建一个静态方法,例如 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;
}

然后您可以使用它

MyFormsResult result = MyForm.DoShowGetResult();

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

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

which would look something like this

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();
只是偏爱你 2024-10-01 05:57:40

试试这个,它似乎对我有用:

 public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public DialogResult ShowDialog(string mes)
        {
            this.textBox1.Text = mes;
            return base.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }

Try this, it seems to work for me:

 public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public DialogResult ShowDialog(string mes)
        {
            this.textBox1.Text = mes;
            return base.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
痴骨ら 2024-10-01 05:57:40

ShowDialog 方法无法被重写。不过,您可以做的是创建一个新方法,它返回 ShowDialog 结果和另一个值。

public ShowDialogResult ShowDialogWrappe(out MyFormResults result) { 
  var dialogRet = ShowDialog();
  result = MyFormResults.Result1;
  return dialogRet;
}

The ShowDialog method cannot be overriden. What you could do intead though is create a new method which returns both the ShowDialog result and another value.

public ShowDialogResult ShowDialogWrappe(out MyFormResults result) { 
  var dialogRet = ShowDialog();
  result = MyFormResults.Result1;
  return dialogRet;
}
深海少女心 2024-10-01 05:57:40

不,这是不可能的。通常的解决方法是将您的真实结果公开为 Form: 上的属性

public MyFormResults MyResult
{
    get;
}

,然后您将设置此:

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?
}

并且调用代码通常如下所示:

if (form.ShowDialog == DialogResult.OK)
{
    //do something with form.MyResult
}

No, it's not possible. The usual workaround is to expose your real result as a property on the Form:

public MyFormResults MyResult
{
    get;
}

and you would then set this:

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?
}

and the calling code usually looks like this:

if (form.ShowDialog == DialogResult.OK)
{
    //do something with form.MyResult
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文