Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a standard way to return values from custom dialogs in Windows Forms?

So right now my project has a few custom dialogs that do things like prompt the user for his birthday, or whatever. Right now they're just doing things like setting a this.Birthday property once they get an answer (which is of type DateTime?, with the null indicating a "Cancel"). Then the caller inspects the Birthday property of the dialog it created to figure out what the user answered.

My question is, is there a more standard pattern for doing stuff like this? I know we can set this.DialogResult for basic OK/Cancel stuff, but is there a more general way in Windows Forms for a form to indicate "here's the data I collected"?

like image 623
Domenic Avatar asked Aug 31 '08 17:08

Domenic


3 Answers

I would say exposing properties on your custom dialog is the idiomatic way to go because that is how standard dialogs (like the Select/OpenFileDialog) do it. Someone could argue it is more explicit and intention revealing to have a ShowBirthdayDialog() method that returns the result you're looking for, but following the framework's pattern is probably the wise way to go.

like image 78
flipdoubt Avatar answered Oct 20 '22 01:10

flipdoubt


is there a more standard pattern for doing stuff like this?

No, it sounds like you're using the right approach.

If the dialog returns DialogResult.OK, assume that all the necessary properties in the dialog are valid.

like image 31
Chris Karcher Avatar answered Oct 20 '22 00:10

Chris Karcher


For me sticking with the Dialog returning the standard dialog responses and then accessing the results via properties is the way to go.

Two good reasons from where I sit:

  1. Consistency - you're always doing the same thing with a dialog and the very nature of the question suggests that patterns are good (-: Although equally the question is whether this is a good pattern?
  2. It allows for return of multiple values from the dialog - ok there's whole new discussion here too but applied pragmatism means that this is what one wants in some circumstances its not always appropriate or desirable to package values up just so that you can pass them back in all in one go.

The flow of logic is nice too:

if (Dialog == Ok)
{
    // Do Stuff with the entered values
}
else
{
    // Respond appropriately to the user cancelling the dialog
}

Its a good question - we're supposed to question stuff like this - but for me the current pattern is a decent one.

Murph

like image 38
Murph Avatar answered Oct 20 '22 00:10

Murph