Opening and saving files: an example – PB Docs 150

Opening and saving files: an example

This example consists of several scripts that handle opening
and saving files. Users can open existing files and save changes.
They can also save the contents to another file. If users save the
file they opened, saving proceeds without interrupting the user.
If users save to a file name that exists, but is not the file they
opened, they are asked whether to overwrite the file:

dartsave.gif

The example includes instance variable declarations, scripts,
functions, and events.

Instance variable declarations

ib_saveas

A flag for the FileExists event. When FALSE,
the user is saving to the file that was opened, so overwriting is
expected:

is_filename

The current file name for the contents, initially set to “Untitled”:

Open Document script

This script opens a file chosen by the user. Since opening
a file triggers the Modified event and sets the Modified property,
the script resets Modified to FALSE. The Checked
property of the Modified check box is set to FALSE too:

Scripts that save the document

The user might choose to save the document to the same name
or to a new name. These scripts could be assigned to menu items
as well as buttons. The Save button script checks whether the instance
variable is_filename holds a valid
name. If so, it passes that file name to the of_save function.
If not, it triggers the SaveAs button’s script instead:

The SaveAs script sets the instance variable ib_saveas so
that the FileExists event, if triggered, knows to ask about overwriting
the file. It calls of_getfilename to
prompt for a file name before passing that file name to the of_save function.

Functions for saving and getting a file name

of_save function

This function accepts a file name argument and saves the document.
It updates the file name instance variable with the new name and sets
the check box to correspond with the Modified property, which is automatically
set to FALSE after you call SaveDocument successfully:

of_getfilename function

The function prompts the user for a name and returns the file
name the user selects. It is called when a file name has not yet been
specified or when the user chooses Save As. It returns a file name:

Events for saving and closing

FileExists event

When the user has selected a file name and the file already exists,
this script warns the user and allows the save to be canceled. The
event occurs when SaveDocument tries to save
a file and it already exists. The script checks whether ib_saveas is TRUE and,
if so, asks if the user wants to proceed with overwriting the existing
file:

Modified event

This script sets a check box so the user can see that changes have
not been saved. The Modified property is set automatically when
the event occurs. The event is triggered when the first change is
made to the contents of the control:

CloseQuery event

This script for the window’s CloseQuery event checks whether
the control has unsaved changes and asks whether to save the document
before the window closes:


Document get from Powerbuilder help
Thank you for watching.
Was this article helpful?
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x