views:

74

answers:

3

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.

+1  A: 

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();
PostMan
A: 

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;
}
JaredPar
+1  A: 

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

pubic MyFormResults MyResult
{
  get;
}

and you would 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
}
Grzenio