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

So now, my project has a few custom dialogs that do things like asking a user for a birthday or something else. Now they just do things like setting the this.Birthday property when they get a response (which is of type DateTime? With a value of zero, indicating "Cancel"). The caller then checks the Birthday property of the created dialog to find out what the user answered.

My question is: is there a more standard template for creating such things? I know that we can set this.DialogResult for the main OK / Cancel file, but is there a more general way in Windows Forms for a form to indicate "here is the data I collected"?

+6
user-interface winforms
source share
5 answers

I would say that setting properties in your custom dialog is an idiomatic way, because standard dialogs (for example, Select / OpenFileDialog) do this. Some might argue that this is more explicit and intent, showing that the ShowBirthdayDialog () method returns the result you are looking for, but following the pattern of the structure is probably a wise way.

+9
source share

is there a more standard template for creating such things?

No, it seems you are using the right approach.

If the dialog box returns DialogResult.OK, suppose all the necessary properties in the dialog box are valid.

+3
source share

For me, sticking to a dialog that returns standard answers to the dialog, and then accessing the results through properties is the way to go.

Two good reasons I'm sitting:

  • Consistency - you always do the same with the dialogue, and the very nature of the question suggests that the templates are good (-: Although the question is the same, is this a good template?
  • It allows you to return several values ​​from the dialogue - well, there is also a new discussion here, but the pragmatism used means that it is in this case that it is required that in some cases it is not always advisable or desirable to pack the values ​​only so that you can pass them back all in one go.

The logic flow is good too:

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

Good question - we should ask such things, but for me the current template is worthy.

Murph

+2
source share

For modal input dialogs, I usually overload ShowDialog and pass parameters for the data I need.

 DialogResult ShowDialog(out datetime birthday) 

I usually found it easier to discover and understand than to mix my properties with the 100+ that the Form class provides.

For forms, I usually have a Controller and IView interface that uses readonly properties to pass data.

+1
source share

I always did it exactly as you describe. I am curious to know if there is a more acceptable approach.

0
source share

All Articles