是否可以重载表单的 ShowDialog 方法并返回不同的结果?
编辑:这个方法实际上效果很好,我问了它然后找到了解决方案。我在重载的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编辑:改变
ShowDialog()
的功能可能不是一个好主意,人们期望它返回DialogResult
并显示表单,我建议像下面的建议一样。因此,仍然允许以正常方式使用ShowDialog()
。您可以在
MyForm
上创建一个静态方法,例如DoShowGetResult()
,它看起来像这样,
然后您可以使用它
Edit: It's proberly not a good idea to change the functionality of
ShowDialog()
, people expect it to return aDialogResult
and show the form, I suggest something like my suggestion below. Thus, still allowingShowDialog()
to be used the normal manner.You could create a static method on your
MyForm
, something likeDoShowGetResult()
which would look something like this
then you can use this
试试这个,它似乎对我有用:
Try this, it seems to work for me:
ShowDialog
方法无法被重写。不过,您可以做的是创建一个新方法,它返回 ShowDialog 结果和另一个值。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.不,这是不可能的。通常的解决方法是将您的真实结果公开为 Form: 上的属性
,然后您将设置此:
并且调用代码通常如下所示:
No, it's not possible. The usual workaround is to expose your real result as a property on the Form:
and you would then set this:
and the calling code usually looks like this: