Dialog and variables

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I am looking for some infomation about dialogs and passing / sharring
variables best practices.

Q1 - What is the best way to pass variables to a dialog form from a main
form? Constructor, properties?

Q2 - What is the best way to pass the updated variables back to the main form?
Properties, Events?

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?

Also any references to docs would be a big help.

Thanks,
Shawn
 
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.
 
In addition to my previous post, always keep in mind the difference between
modal and non-modal forms:

- If your form is shown modally, setting its DialogResult property causes
the form to be automatically hidden (not closed) and the ShowDialog method
to return. This of course doeesn't happen if the form is shown non-modally
(with the Show method)
- When a form is shown modally, clicking the upper-right Close cross button
causes the form to be hidden (not closed), ShowDialog returns and the
form's DialogResult is set to Cancel. Doing the same thing on a non-modal
dialog causes the dialog to be closed and disposed.
- Because of the 2 points above, once the ShownDialog has returned and
after you have retrieved all the values you need, remember to always call
the Close() or Dispose() method of the form in order to free the ressources
held by it. Unless of course you want to show this form again later. Once
again, this only applies to form shown modally as non-modal forms are
autmatically disposed when closed.
 
Shawn,
I am looking for some infomation about dialogs and passing / sharring
variables best practices.

Q1 - What is the best way to pass variables to a dialog form from a main
form? Constructor, properties?

Either ways is good. Maybe it is even better to have both and leave the
programmer to choose. What I believe is important though is that you provide
default property for all the variables so the user can use a simple
contructor - 2 paramters at the most and have the most common configuration
for the dialog already set up.

I think is a goot practice not to use contructor for intialization that may
throw an exception. If the exception can happen during initialization I'd
preffer to use a special Initialize method.
Q2 - What is the best way to pass the updated variables back to the main
form?
Properties, Events?

Depends where the dialog is modal or modeless. Modeless dialogs pass thier
values via events because they execute concurent to the rest of the code. At
least the needs to fire an event that some of the buttons are clicked. In
that event the dialog may pass its values or if the values are many it can
send reference to itself (it does kind of in the *sender* parameter) and let
the event handler read its properties.

For modal dialogs you don't need events since the code calling ShowDialog is
blocked on this call until the dialog is dismissed.
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

Again, modal dialog doesn't need any notifications the usual code for
showing modal dialogs is:

using(MyDialog dlg = new MyDilaog(<could have some paramters here>)
{
// Dialog initiazlization if needed.
dlg.XXXX =
dlg.YYYY =
dlg.ZZZZZ =
if(dlg.ShowDialog() == DialogResult.Ok)
{
// Consume data from dialogs public properties

}
}

For modeless dialogs you need to have event notifying about button click.

BTW modal dialogs are the one shown using ShowDialog method where modeless
dialogs are shown suing the Show method.
 
Back
Top