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"?
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.
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.
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With