Switch to standard view 
  Sybase logo

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 functions.
Two attributes called start and stop are both variables of type time and are used to record start and stop times.

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 variables:
private time start,stop

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 body of function stop_timer is: stop=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.

nvuo_timer stopwatch

Then in the application open event we have:
stopwatch = create nvuo_timer // create an object of class type nvuo_timer
open(w_stopwatch) // open the GUI

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:

stopwatch.start_timer()  //cb_start_timer::clicked event
stopwatch.stop_timer()   //cb_stop_timer::clicked event
long elapsed_time        //cb_elapsed_time::clicked event
elapsed_time = stopwatch.elapsed_time()    //cb_elapsed_time::clicked event
messagebox("Elapsed Seconds",elapsed_time) //cb_elapsed_time::clicked event

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
end type
end forward

global type nvuo_timer from nonvisualobject
end type
global nvuo_timer nvuo_timer

type variables
private time start, stop
end variables

forward prototypes
public subroutine start_timer ()
public subroutine stop_timer ()
public function long elapsed_time ()
end prototypes

public subroutine start_timer ();start=now()
end subroutine

public subroutine stop_timer ();stop=now()
end subroutine

public function long elapsed_time ();return(secondsafter(start,stop))
end function

on nvuo_timer.create
TriggerEvent( this, "constructor" )
end on

on nvuo_timer.destroy
TriggerEvent( this, "destructor" )
end on

Back to Top
© Copyright 2010, Sybase Inc.