Try OpenEdge Now
skip to main content
GUI for .NET Programming
Overview : Simple example
 

Simple example

The following sample procedure, ShowDateTime.p, is a simple ABL application that accesses .NET objects. It makes use of some of the basic ABL features that support access to .NET objects, including an OpenEdge .NET class and two from among a set in-the-box controls that OpenEdge installs to support the GUI for .NET.
USING Infragistics.Win.Misc.* FROM ASSEMBLY.                        /* 1 */
USING System.Windows.Forms.* FROM ASSEMBLY.

/* Define object reference and data variables */                    /* 2 */
DEFINE VARIABLE rDateForm AS CLASS Progress.Windows.Form NO-UNDO.
DEFINE VARIABLE rOKButton AS CLASS UltraButton NO-UNDO.
DEFINE VARIABLE rDateField AS CLASS UltraLabel NO-UNDO.

/* Create objects */                                                /* 3 */
rDateForm = NEW Progress.Windows.Form( ).
rOKButton = NEW UltraButton( ).
rDateField = NEW UltraLabel( ).

/* Initialize OK button */                                          /* 4 */
rOKButton:Text = "OK".
rOKButton:Size = TextRenderer:MeasureText( "OK",
                                           rOKButton:Font ).
rOKButton:Height = rOKButton:Height + 12.
rOKButton:Width = rOKButton:Width + 12.
rOKButton:DialogResult = DialogResult:OK.
rOKButton:Top = rDateField:Top + rDateField:Height + 4.

/* Initialize current date/time field */                            /* 5 */
rDateField:Text = STRING(System.DateTime:Now).
rDateField:Size = TextRenderer:MeasureText( rDateField:Text,
                                            rDateField:Font ).
rDateField:Top = 2.

/* Initialize dialog with field and button */                       /* 6 */
rDateForm:Text = "Today's Date and Time".
rDateForm:MaximizeBox = FALSE.
rDateForm:MinimizeBox = FALSE.
rDateForm:FormBorderStyle = FormBorderStyle:FixedDialog.
rDateForm:Controls:Add( rDateField ).
rDateForm:Controls:Add( rOKButton ).
rDateForm:AcceptButton = rOKButton.

/* Adjust dialog size and controls for field and button */          /* 7 */
rDateForm:Width = rDateField:Width * 1.5.
rDateForm:Height = rOKButton:Top + rOKButton:Height +
                   ( 2 * rDateField:Height ) + 24.
rDateField:Left = ( rDateForm:Width - rDateField:Width ) / 2.
rOKButton:Left = ( rDateForm:Width - rOKButton:Width ) / 2.

/* Show dialog and wait for button click */                         /* 8 */
WAIT-FOR rDateForm:ShowDialog( ).

rDateForm:Dispose( ).                                               /* 9 */
For information on compiling and running this procedure, see Example procedures.
When you run ShowDateTime.p, it creates and displays a dialog box that shows the current date and time, as shown in the following figure.
Figure 3. Simple application accessing .NET objects
The following is a description of the ABL elements used to implement this procedure, numbered according to the numbered comments in the sample code:
1. The USING statements specify .NET namespaces (similar to ABL packages) in which some of the referenced .NET object types are defined. This allows you to reference the object type by its unqualified class or interface name. The FROM ASSEMBLY option tells ABL to search .NET assemblies (rather than ABL packages on PROPATH) for the definitions of types defined in these namespaces.
Note: While sometimes named alike, .NET namespaces and assemblies are not the same thing. Assemblies are the physical files that contain actual type definitions, while namespaces are logical groups of types. Types in the same namespace can be defined in different assemblies, and a single assembly can define types in different namespaces.
2. This code defines object reference variables for the classes used in the procedure. The Progress.Windows.Form class is an OpenEdge .NET class that inherits from the Microsoft System.Windows.Forms.Form class and provides a .NET form object that is designed for use in an ABL session. The UltraButton and UltraLabel classes (from the Infragistics.Win.Misc namespace) are two of the Ultra Controls from Infragistics® installed with this OpenEdge release. The UltraButton provides a button similar to the Microsoft System.Windows.Forms.Button class and the UltraLabel provides a fill-in for displaying data, similar to the System.Windows.Forms.Label class.
Note: For more information on Progress.Windows.Form, see Creating and Using Forms and Controls.
3. The NEW function (classes) instantiates these .NET classes just like an ABL user-defined class.
4. To initialize the button object, the procedure assigns ABL data type values to selected UltraButton class properties. However, the data type of the DialogResult property is the .NET System.Windows.Forms.DialogResult enumeration. Thus, the procedure sets this property to the OK member of the DialogResult enumeration class.
Note: The DialogResult property has the same name as its data type, which is the DialogResult enumeration type. Although the names are the same, they do refer to two different things.
5. To initialize the date/time field object, the procedure assigns ABL data type values to selected UltraLabel class properties. It gets the current date and time from the static Now property of the System.DateTime class.
Note: You can also obtain a similar result more efficiently using the NOW ABL built-in function, which also includes the time zone. However, this example uses the .NET property to demonstrate access to the .NET feature.
6. To initialize the dialog box object, the procedure assigns ABL data type values to selected Progress.Windows.Form class properties, which are inherited from System.Windows.Forms.Form. It also adds the UltraLabel and UltraButton controls to the form by invoking the Add( ) method on the form Controls property. This property has the .NET type, System.Windows.Forms.Control+ControlCollection, which is an inner class of the Control class. Because the Form class inherits Control, the form object can use this property to contain a collection of controls that the form can display in its client area.
7. After the form is resized according to the control dimensions, the procedure repositions the controls to center them in the form client area.
8. Once the dialog box object is initialized, the procedure blocks for input using a .NET-specific form of the WAIT-FOR statement for input-blocking .NET methods. The syntax for this WAIT-FOR statement invokes the .NET form method, ShowDialog( ), which displays the dialog box as well as blocking for input. The statement then blocks until you click the OK button or press the ENTER key. This causes the button object to publish a Click event, during which .NET automatically sets the DialogResult property of the form to the enumeration value (DialogResult:OK) of the button's own DialogResult property. Anything that sets the DialogResult property on a form displayed as a dialog box automatically causes the dialog box to close and the ShowDialog( ) method to return that same value, which also terminates the WAIT-FOR statement. If ShowDateTime.p needed to check the method return value, the .NET WAIT-FOR statement syntax also provides an option (SET) that conveniently makes this return value available to the procedure.
9. .NET garbage collects objects in much the same way that OpenEdge garbage collects ABL class instances. However, .NET dialog boxes can be closed in a way that prevents them from being garbage collected in a timely manner. Therefore, to ensure garbage collection of a .NET dialog box object and to avoid the application memory leaks that it can create, you must call the .NET Dispose( ) method on the object when you no longer need it. Calling this method is unnecessary, however, for most other .NET objects.
The remaining chapters of this book describe all of the features shown here (and more) for working with .NET objects in an ABL session.
Note: You can locate the third-party documentation for the .NET objects referenced in this example as follows. For the OpenEdge .NET class, Progress.Windows.Form, see the reference entry for this class in OpenEdge Development: ABL Reference. For the OpenEdge Ultra Controls for .NET added to the .NET form, see OpenEdgeUltra Controls for .NET.