Is it possible to overload the ShowDialog method for forms and return a different result?

前端 未结 4 2521
渐次进展
渐次进展 2021-02-20 06:04

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 over

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-20 06:24

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

提交回复
热议问题