Revisiting our Simple Form
- Last UpdatedOct 29, 2024
- 3 minute read
Our simple form !!Hello, which you have constructed earlier in this section, is not very intelligent. Once you have clicked the Change button, the .Message paragraph will read 'Modified' for ever more, even if you hide the form and re-show it.
The extended version of form !!Hello below:
-
Illustrates the use of the form definition file.
-
Illustrates form methods as callbacks.
-
Introduces some important predefined form members.
First save the definition in a file called hello.pmlfrm and make sure that its directory is in your PMLLIB search-path.
layout form !!hello AutoAlign
title ‘Display Your Message’
paragraph .Message width 15 height 1
text .capture ‘Enter message’ width 15 is STRING
button .bye 'Goodbye' OK
exit
define method .hello()
-- default constructor - set gadget load-time default values
!this.message.val = ‘Hello world’
!this.capture.callback = ‘!this.message.val = !this.capture.val’
!this.Okcall = ‘!this.success()’
endmethod
define method .success()
-- action when OK button is pressed
!this.message.val = ‘Hello again’
!this.capture.val = ‘’
endmethod

In the form definition:
-
title sets the formtitle member and hence displays a title.
-
para adds a PARAGRAPH gadget size 15 chars by 1 line with no content.
-
text adds a TEXT field gadget with tag 'Enter message', width 15 chars, to hold data of type STRING.
The constructor method .hello() does the following:
-
It initializes the default value of the paragraph to 'Hello world'.
-
It defines the callback on the text input field: to insert its value into the paragraph.
-
Sets the form member Okcall (in the line beginning !this.Okcall). This is a callback that gets executed when a button with control-type OK is pressed.
The definition of method .success() does the following:
-
Sets the value of the paragraph to 'Hello again'.
-
Resets the value of the text field to empty.
Now load and show the form by typing:
PML rehash
show !!Hello
This will auto-load the form definition and execute the default constructor method (which will set the message paragraph to 'Hello world' and define the gadget and form callbacks). It will also display the form.
Type a message into the Enter message field and press the Enter key. This will execute the callback of the field, which will write the typed text into the message paragraph.
Type a new message and press Enter. The paragraph updates to reflect what you have typed.
Click the Goodbye button. This executes the Okcall action of the form, which calls the success() method. The success() method sets the paragraph to 'Hello again' and blanks out the text field. Finally, the OK control action hides the form.
Show the form again and observe that the paragraph reads 'Hello again' and not 'Hello world'. This demonstrates that when you re-show the form the form’s constructor is not run, because the form is already loaded.
If you want to reset the form every time it is shown, you must define a form initialization callback, refer to Form Initialization Callback.