Using PowerBuilder Non-Visual User Objects - A Stopwatch Application
This example illustrates how to use a non-visual user object (nvuo) in PowerBuilder to build a simple stopwatch application that can be used to time your application.
The nvuo will contain two private attributes and three public
The three public nvuo functions are:
To create the nvuo, enter the User Object Painter, choose "new" and
then "custom" for your object type. Then declare the following instance
Then create the user object functions start_timer() and stop_timer(). Both functions have no arguments and both do not return any return argument.
The body of function start_timer() is simply: start=now()
The function elapsed_time() takes no arguments but returns a variable of type long. The body of function elapsed_time is: return (secondsafter(start,stop))
Save the user object as nvuo_timer. Now that the user object nvuo_timer has been created, we need a global variable of class type nvuo_timer so that we can use the non-visual user object from any object in our application.
Then in the application open event we have:
The user interface consists of a single window which we will name w_stopwatch. The window w_stopwatch contains three command buttons,namely, cb_start_timer, cb_stop_timer and cb_elapsed_time. The code behind the clicked event for each of these command buttons is shown as follows:
Now you are ready to run the application. Simply click on command button labeled "Start Timer", then click on "Stop Timer", then click on "Elapsed Time" to view the elapsed time.
You can also place calls directly within Powerscript. For example, you could place a call to stopwatch.start_timer in the open event of your window, then place a call to stopwatch.stop_timer in the close event of the window to record the total time that window was open. To view the elapsed time, simply place a call to stopwatch.elapsed_time and display the value in a message box.
Here is the exported syntax for the non-visual user object, nvuo_timer:
global type nvuo_timer from nonvisualobject
public subroutine start_timer ();start=now()
public subroutine stop_timer ();stop=now()
public function long elapsed_time ();return(secondsafter(start,stop))