Block on modal dialog boxes
- Last Updated: October 21, 2022
- 5 minute read
- OpenEdge
- Version 13.0
- Documentation
Syntax
When you display a form as a modal dialog box, the user cannot give focus to any other form in your application until they somehow close the dialog box. You can create modal forms from one of the following dialog box classes:
-
System.Windows.Forms.Form(or a derived class), especiallyProgress.Windows.Form) — OpenEdge provides the derived .NET class, Progress.Windows.Form, which is specifically designed to work within an ABL session, allowing you either to create modal dialog boxes directly from it or to create modal dialog boxes using ABL classes that you further derive from it. This class provides the same features for forms displayed as modal dialog boxes that are provided for forms displayed as non-modal forms. In addition, it provides features especially for use by dialog box forms. -
.NET forms derived from the abstract class,
System.Windows.Forms.CommonDialog—System.Windows.Forms.CommonDialogis the base class for a set of special-purpose dialog boxes provided by the .NET Framework, including among others, theSystem.Windows.Forms.FileDialogclass to implement a file selection dialog box, and theSystem.Windows.Forms.ColorDialogclass to implement a color selection dialog box. These classes have features oriented around a specialized use as compared to the richer set of general-purpose features available with theSystem.Windows.Forms.Formclass and its subclasses.
To display and block on a modal
dialog box, execute a .NET WAIT-FOR statement
that calls the ShowDialog( ) method on
the dialog box class instance. This is the syntax for using the .NET WAIT-FOR statement to call the ShowDialog( ) method:
|
The dialog-object-ref is
an object reference to the form class instance you want displayed
as a modal dialog box. The owning-form-ref is
an object reference to a form that owns the dialog box (that is,
a form over which the dialog box displays). Note that you cannot
set the dialog-object-ref to be visible by calling
its Show( ) method prior to executing
this statement, as you might with a non-modal form. If you do, .NET raises
an error when you execute this WAIT-FOR statement.
The ShowDialog( ) method available on
any dialog-object-ref returns a value of type System.Windows.Forms.DialogResult.
This is an enumeration type that indicates the result from closing
the dialog box. If you want to check this result, you can use the SET option, which
sets a variable (return-value) to the value returned by
the method. On some dialog form classes, you can also check the DialogResult property
(with the same name as its type), which contains the same value.
For Progress.Windows.Form objects,
you can set the DialogResult property in an event
handler or rely on .NET to set the value from one or more
button controls that you add to the dialog box. Every button control
also has a DialogResult property, and .NET automatically
sets the dialog box property to the value of the DialogResult property
of the button that the user clicks in the dialog box. Either way,
setting the value of DialogResult on the dialog
box form object causes .NET to close the dialog box. You can
then check the property value after the dialog box closes following
termination of the WAIT-FOR statement.
X) button on a dialog box, or
when you set the value of the dialog-object-ref:DialogResult property, .NET does not automatically call the
Close( ) method on dialog-object-ref, and therefore does not also call the Dispose( ) method on the form. Instead, .NET hides the form so it can be shown
again without having to create a new instance of the dialog box. Thus, when a modal form is
no longer needed by your application, you must explicitly call the Dispose( ) method on dialog-object-ref (and
call it before any invocation of the DELETE OBJECT statement on
dialog-object-ref) to ensure that the form and all the
.NET controls that it contains are garbage collected. Otherwise, your .NET dialog boxes can
create memory leaks in your application.If you
use a Progress.Windows.Form (or another System.Windows.Forms.Form class) instance
to create a dialog box, you have access to the full range of methods, properties,
and events that are available for this class, including the DialogResult property.
This property is not available on System.Windows.Forms.CommonDialog objects.
However, you can check the DialogResult value for
dialog boxes displayed for a CommonDialog form
by using the SET option.
WAIT-FOR statement for displaying all
non-modal .NET forms and ABL windows, you can execute a WAIT-FOR statement
to display a dialog box (.NET or ABL) anywhere in your application,
and in any order that you choose. For more information, see Use .NET forms with ABL windows.The following MixedForms.p procedure
defines two non-modal forms (rForm1 and rForm2) and one modal dialog box (rDialog1) that displays when you click on either one of the forms.
|
In MixedForms.p, the Form_Click event
handler defines and manages the dialog box and its System.Windows.Forms.Button controls
(rCloseButton and rCancelButton).
The handler sets the DialogResult property of each
button to a different DialogResult enumeration
value. It also sizes the client area (ClientSize property)
of the dialog box based on the sizes of these buttons. It then adds
and centers the buttons in the dialog box client area based on these
dimensions. Note also the use of the FormBorderStyle property
to specify a fixed dialog style from the System.Windows.Forms.FormBorderStyle enumeration.
The ShowDialog( ) method
called in the WAIT-FOR statement then
blocks and displays the dialog box. If the user clicks the rCloseButton control,
the rCancelButton control, or the Close (X) button on the title bar,
the dialog box closes with the DialogResult property
on rDialog1 set to the clicked button's DialogResult property
value or to DialogResult:Cancel for the Close (X) button.
After ShowDialog( ) returns,
the WAIT-FOR statement terminates and sets enDialogResult with
the method return value (which is also the value of the DialogResult property).
The event handler then checks the value of enDialogResult using
the static AreEqual( ) method of the Progress.Util.EnumHelper class. If the
value is DialogResult:OK, the handler calls the Close( ) method
on Form1. This causes the Application:Run( ) method
to return from the non-modal WAIT-FOR statement
in the main block, terminating the procedure. If the value is other
than DialogResult:OK, the event handler assumes
that the dialog box was cancelled, and closes, allowing the non-modal
forms to continue being displayed. Note also that the handler calls Dispose( ) on rDialog1 to ensure
that the dialog box is garbage collected.
For the System.Windows.Forms.CommonDialog classes, you
can use the ShowDialog( ) method in much
the same way as for Progress.Windows.Form.
However, each CommonDialog class also provides
its own set of class members built on an inheritance hierarchy based
on CommonDialog. These dialog boxes do not provide DialogResult and
many other properties provided by System.Windows.Forms.Form,
including a Controls property to add controls.
Befitting their application orientation, these dialog boxes are
standardized for special-purpose use and manage all of the controls
that they use as a built-in feature of each CommonDialog class.
Because
there is no DialogResult property on these dialog
boxes, to test the result of a CommonDialog object,
you must use one of the following mechanisms:
- Use the
SEToption on theWAIT-FORstatement, as noted previously, to return theDialogResultvalue fromShowDialog( ). - Query a property on the given
CommonDialogobject that changes based on the result. - Use an event handler on a suitable subclass event, such as the
FileOkevent on theOpenFileDialogsubclass, to handle the results.