I am looking for some infomation about dialogs and passing / sharring
variables best practices.
I don't know of any best practice in this area (although there maybe are).
The most important IMHO is to choose a programming style and stick to it
within the whole project so that you won't have later to try and figure out
how this particular form should be initialized.
Q1 - What is the best way to pass variables to a dialog form from a main
form? Constructor, properties?
I usually like to have an Initialize method that takes parameters and
allows you to set all the required values. But that's just me. If you have
plenty of optional values that the caller is free to either set or let at
their default values it may make more sense to expose them as public
properties and let the caller set them if required instead of forcing the
caller to supply a value through an Initialize method or an overloaded
constructor.
Q2 - What is the best way to pass the updated variables back to the main form?
Properties, Events?
Depends. If you want to react to the value changes immediately before the
dialog closes (for instance a "Volume Control" pop up dialog box that lets
the user change the volume), then you'll probably want to create custom
events and fire them as soon as the user changes the value in the dialog
box. If it's OK to wait until the user closes the dialog box before taking
the new values into account, then public properties makes perfect sense to
me.
Q3 - What is the best way to the main form to be notified when the dialog
has its close / save button clicked?
ShowDialog, Trigger an event, and have the main form read the public
properties and close the dialog?
Depends again. If you have shown your dialog modally with ShowDialog, have
your "Save" and "Cancel" buttons set the DialogResult property of your form
(note that you can do that directly in the designer. Look for the
DialogResult property of your buttons. Also take a look at the
documentation of the DialogResult property of a Form). Then the caller can
check the result of the ShowDialog method to know how the form has been
closed. Note that the built-in close button in the menu bar of a form (the
big red cross under windows XP) automatically closes the form at sets its
DialogResult to Cancel. Once the form is closed you can then use public
properties to allow the main form to read the data.
Things are different with non-modal forms since you can't use the
DialogResult property. In this case, you could either create custom events
to notify the form that the user wants to close the form as you said. Or
have the main form handle the Closing event of your dialog and then reaad
public properties. Be careful though, unlike modal forms, non-modal forms
are automatically disposed when closed.