Try OpenEdge Now
skip to main content
GUI for .NET Programming
Creating and Using Forms and Controls : Creating custom .NET forms and controls : Sample ABL-derived .NET modal dialog box
 

Sample ABL-derived .NET modal dialog box

UTCDialog is a sample ABL-derived modal dialog box class that allows you to select a time zone used to display the current time, displayed as in the following figure.
Figure 6. Modal form (dialog box) displayed for UTCDialog.cls
The dialog box displays two buttons, UTC or EST? and OK, and a label field displaying a message that indicates which time zone is selected, Eastern Standard Time (EST) or Coordinated Universal Time (UTC). When you click the UTC or EST? button, the label field toggles between one message and the other and sets a flag to indicate the selected time zone. When the you click OK, this closes the dialog box with a result that indicates the selected time zone.
Note: In Progress Developer Studio for OpenEdge, you can create ABL-derived modal dialog boxes to design in Visual Designer. You can initiate creation of a new modal dialog box by clicking on File > New > ABL Dialog.
The sample ABL-derived non-modal form class, UTCSelectForm, creates an instance of the UTCDialog class and displays its modal dialog box. This non-modal form class is similar to the CurrentTimeForm class described in Sample ABL-derived .NET non-modal form, and displays as in the following figure.
Figure 7. Non-modal form displayed for UTCSelectForm.cls
In this case, the Close button of CurrentTimeForm is replaced by a Select Zone button in UTCSelectForm. The non-modal form displays the current date and time with an indication of the time zone (UTC or EST). When you click the Select Zone button, this launches the UTCDialog instance where you can select the time zone for the current time. When you close the dialog box with a selection, the form now displays the time in the specified time zone, along with the appropriate time zone indication.
Note: Like CurrentTimeForm, UTCSelectForm makes its non-modal form the main form of the application. So, clicking the Close (X) button on the form implicitly invokes the Close( ) method on the form, which both closes the main form and terminates the application.
You can launch these sample classes using the sample procedure, UTCSelectFormDriver.p., which is similar to the CurrentTimeDriver.p procedure described in the previous section. For information on locating and running these samples, see Example procedures.
The UTCSelectForm class launches its UTCDialog instance in an event handler for the Select Zone button (TZButton_Click). The following fragment of UTCSelectForm shows the TZButton_Click event handler and its supporting code.
USING Infragistics.Win.Misc.* FROM ASSEMBLY.
USING System.Windows.Forms.* FROM ASSEMBLY.
USING Progress.Util.* FROM ASSEMBLY.

CLASS UTCSelectForm INHERITS Progress.Windows.Form:
  
/* Variables for controls on form */
  . . .
  DEFINE PRIVATE VARIABLE rTZButton AS CLASS UltraButton NO-UNDO.
  DEFINE PRIVATE VARIABLE rDateField AS CLASS UltraLabel NO-UNDO.
  DEFINE PRIVATE VARIABLE lUTCSelected AS LOGICAL INITIAL FALSE NO-UNDO.
  . . .

  METHOD PRIVATE VOID TZButton_Click( sender AS System.Object,
                                      e AS System.EventArgs ):
    DEFINE VARIABLE rDialog AS CLASS UTCDialog NO-UNDO.
    DEFINE VARIABLE enResult AS CLASS DialogResult NO-UNDO.
    rDialog = NEW UTCDialog( lUTCSelected ).
    rDialog:WaitForDialog( INPUT THIS-OBJECT, OUTPUT enResult ).

    rDialog:Dispose( ).

    IF EnumHelper:AreEqual( enResult, DialogResult:Yes )
    THEN
      lUTCSelected = TRUE.
    ELSE
      lUTCSelected = FALSE.
    
    rDateField:Text = GetTime( ).
    rDateField:Size = TextRenderer:MeasureText( rDateField:Text,
                                                rDateField:Font ).
    
  END METHOD.
  
  METHOD PRIVATE CHARACTER GetTime( ):
    DEFINE VARIABLE dTime AS DATETIME NO-UNDO.
    DEFINE VARIABLE iHour AS INTEGER INITIAL 3600000 NO-UNDO.
    
    dTime = NOW.
    IF lUTCSelected    THEN DO:
      dTime = dTime + (5 * iHour).
      RETURN STRING(dTime) + " UTC".
    END.
    ELSE DO:
      RETURN STRING(dTime) + " EST".
    END.
  END METHOD.
TZButton_Click creates the UTCDialog instance referenced by rDialog, and immediately executes the WaitForDialog( ) method on it. This method executes the WAIT-FOR statement to display and block on the dialog box, which is parented to the main form (THIS-OBJECT), passed in as an INPUT parameter. When the method returns, it also returns the enumeration value of the DialogResult property on the dialog box, passed out as an OUTPUT parameter (enResult). In the UTCDialog instance, the value of the DialogResult property is set in a Click event handler to indicate time zone selected in the dialog box.
Note: Instead of using the DialogResult property, the WaitForDialog( ) method could directly return a LOGICAL value for the lUTCSelected data member, which is also set in UTCDialog. However, this example also demonstrates another potential use for the DialogResult property in an application, especially where more complex results might be returned and you want the property to reflect a priority condition.
After the dialog box closes, this method returns, and the event handler tests enResult to determine what time zone has been selected. It then sets a UTCSelectForm class flag (lUTCSelected) accordingly and returns the current date and time, as a string, using the private GetTime( ) method. The GetTime( ) method also tests this flag and creates the date and time string from either the current date and time UTC or the current date and time EST (five hours earlier) and appends the time zone indicator before returning the result. The event handler concludes by setting the Text property of the UltraLabel instance (rDateField), which displays the date and time for the parent form.
Note: After WaitForDialog( ) returns, Dispose( ) is called on rDialog to ensure that the ABL-derived .NET dialog box object is garbage collected. For more information, see Blocking on modal dialog boxes.
This is the initial section of code for the ABL UTCDialog class, where the class private data and public members are defined.
USING System.Windows.Forms.* FROM ASSEMBLY.

CLASS UTCDialog INHERITS Progress.Windows.Form:

  DEFINE PRIVATE VARIABLE buttonTZ AS Button NO-UNDO.
  DEFINE PRIVATE VARIABLE buttonOk AS Button NO-UNDO.
  DEFINE PRIVATE VARIABLE labelTZ AS Label NO-UNDO.
  DEFINE PRIVATE VARIABLE lUTCSelected AS LOGICAL NO-UNDO.

  METHOD PUBLIC VOID WaitForDialog
                       ( INPUT pForm AS CLASS Progress.Windows.Form,
                         OUTPUT pResult AS CLASS DialogResult ):
    WAIT-FOR THIS-OBJECT:ShowDialog( pForm ) SET pResult.
  END METHOD.

  CONSTRUCTOR PUBLIC UTCDialog ( INPUT pUTCSelected AS LOGICAL ):
    lUTCSelected = pUTCSelected.
    InitializeComponent( ).
  END CONSTRUCTOR.
The private data includes object references for the dialog box object and its control objects, and also includes its own class flag (lUTCSelected) to indicate the selected time zone. The public members include the WaitForDialog( ) method that displays and blocks on the dialog box and the UTCDialog class constructor.
The WaitForDialog( ) executes the WAIT-FOR statement calling the ShowDialog( ) instance method for the dialog box. This method takes the INPUT parameter (pForm) that references the parent form for the dialog box, which in this case will be the non-modal form instantiated by UTCSelectForm. It also returns the value of ShowDialog( ) (which is the DialogResult property value on rDialog) as an OUTPUT parameter (pResult) after the corresponding dialog box closes, thus allowing the caller to evaluate the dialog box results.
The constructor also takes a parameter (pUTCSelected) so that the caller can control the initial time zone selection in the dialog box. And as for any similar container class (see Sample ABL-derived .NET non-modal form), its constructor calls a private InitializeComponent( ) method to initialize the dialog box and its controls.
This is the beginning of the InitializeComponent( ) method for UTCDialog, showing the initialization of its controls.
  METHOD PRIVATE VOID InitializeComponent ( ):
    
    buttonTZ = NEW Button ( ).
    buttonOk = NEW Button ( ).
    labelTZ = NEW Label ( ).

    THIS-OBJECT:SuspendLayout ( ).

    IF lUTCSelected
    THEN
      labelTZ:Text = "UTC Time Zone".
    ELSE
      labelTZ:Text = "Eastern Standard Time Zone".
    labelTZ:Size = TextRenderer:MeasureText( labelTZ:Text,
                                             labelTZ:Font ).

    labelTZ:Size = NEW System.Drawing.Size
                         ( INTEGER(labelTZ:Width * 1.1 ),
                           INTEGER(labelTZ:Height * 1.6 ) ).

    labelTZ:Top = 20.

    buttonTZ:Text = "UTC or EST?".
    buttonTZ:TabIndex = 0.
    buttonTZ:Size = NEW System.Drawing.Size
                          ( INTEGER(buttonTZ:Width * 1.2), 23 ).
    buttonTZ:Anchor = System.Windows.Forms.AnchorStyles:Bottom.
    buttonTZ:Top = 20 + labelTZ:Height + 10.
    buttonTZ:Click:Subscribe( tzButton_Click ).

    buttonOk:Text = "OK".
    buttonOk:TabIndex = 1.
    buttonOk:Size = NEW System.Drawing.Size
                          ( INTEGER(buttonOk:Width * 1.1), 23 ).
    buttonOk:Anchor = System.Windows.Forms.AnchorStyles:Bottom.
    buttonOk:Top = buttonTZ:Top.
    buttonOk:Click:Subscribe( okButton_Click ).
Notable initializations for this class include setting the initial label text (labelTZ:Text) to display an indication of currently selected time zone and subscribing event handlers to the Click events on the dialog box buttons that determine the latest time zone selection. Ultimately, the current time zone selection is indicated by the lUTCSelected flag. The value of this flag is initially set from the parameter passed to the UTCDialog class constructor and is reset according to the results of the dialog box.
This is the conclusion of the InitializeComponent( ) method for UTCDialog, showing the dialog box initialization.
    THIS-OBJECT:Text = "Select UTC or Eastern Standard Time".
    THIS-OBJECT:AcceptButton = buttonOk.
    THIS-OBJECT:ClientSize = NEW System.Drawing.Size
                               ( (buttonTZ:Width + buttonOK:Width) * 2,
                                 labelTZ:Height + buttonTZ:Height + 40 ).

    THIS-OBJECT:Controls:Add( labelTZ ).
    THIS-OBJECT:Controls:Add( buttonTZ ).
    THIS-OBJECT:Controls:Add( buttonOk ).
    labelTZ:Left = (THIS-OBJECT:Width - labelTZ:Width) / 2.
    buttonTZ:Left = ( THIS-OBJECT:Width -
                      ( buttonTZ:Width + buttonOk:Width + 10 ) ) / 2.
    buttonOk:Left = buttonTZ:Left + buttonTZ:Width + 10.
    THIS-OBJECT:FormBorderStyle = FormBorderStyle:FixedDialog.
    THIS-OBJECT:MaximizeBox = FALSE.
    THIS-OBJECT:MinimizeBox = FALSE.
    THIS-OBJECT:ShowInTaskbar = FALSE.
    THIS-OBJECT:StartPosition = FormStartPosition:CenterParent.

    THIS-OBJECT:ResumeLayout (FALSE).
  END METHOD.
The last several settings for the dialog box object (THIS-OBJECT) initialization show some typical properties set for a dialog box. Note especially the StartPosition property set to FormStartPosition:CenterParent. This centers the dialog box over its parent form, which in this case will be the non-modal form created by UTCSelectForm, whose object reference (pForm) is passed to the WaitForDialog( ) method.
This is the concluding section of UTCDialog code showing its PRIVATE event handlers.
  METHOD PRIVATE VOID okButton_Click (sender AS System.Object,
                                      e AS System.EventArgs):
    IF lUTCSelected
    THEN
      THIS-OBJECT:DialogResult = DialogResult:Yes.
    ELSE
      THIS-OBJECT:DialogResult = DialogResult:No.
    THIS-OBJECT:Close ( ).
  END METHOD.
  
  METHOD PRIVATE VOID tzButton_Click (sender AS System.Object,
                                      e AS System.EventArgs):
    IF lUTCSelected
    THEN DO:
      lUTCSelected = FALSE.
      labelTZ:Text = "Eastern Standard Time Zone".
    END.
    ELSE DO:
      lUTCSelected = TRUE.
      labelTZ:Text = "UTC Time Zone".
    END.
    labelTZ:Size = TextRenderer:MeasureText( labelTZ:Text,
                                             labelTZ:Font ).
    labelTZ:Size = NEW System.Drawing.Size
                         ( INTEGER(labelTZ:Width * 1.1 ),
                           INTEGER(labelTZ:Height * 1.6 ) ).
    labelTZ:Left = (THIS-OBJECT:Width - labelTZ:Width) / 2.
  END METHOD.

END CLASS.
In the okButton_Click event handler, when the OK button (buttonOK) is clicked, it sets the DialogResult property of the dialog box (THIS-OBJECT) depending on the current value of the class private flag (lUTCSelected), which setting closes the dialog box.
In the tzButton_Click event handler, when the UTC or EST? button (buttonTZ) is clicked, it toggles the current time zone setting by reversing the value of the lUTCSelected flag and setting the Text property of the Label control (labelTZ) to indicate the newly selected time zone. Note that the label is resized and re-centered in the dialog box according to its current Text property value and font.