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

前端 未结 4 2549
渐次进展
渐次进展 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:18

    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
    }
    

提交回复
热议问题