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

0

本文介绍了是否可以重载表单的 ShowDialog 方法并返回不同的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

我有一个表单,您可以在其中单击三个按钮之一.我为返回的结果定义了一个枚举.我想打电话:

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?

谢谢.

推荐答案

更改 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;
}

那你就可以用这个了

MyFormsResult result = MyForm.DoShowGetResult();

这篇关于是否可以重载表单的 ShowDialog 方法并返回不同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14