Defining a custom toolbar within a form uses much of the same basic code described above for defining and creating a reusable toolbar. The primary difference is that the toolbar is available only to the form in which it is defined.

Here’s how the same toolbar described above could be adapted for use within a single form:

** END HEADER -- do not remove this line

*

* Generated on 08/20/97

*

parameter bModal

local f

f = new tooltestForm( )

if (bModal)

f.mdi = .F. // ensure not MDI 

f.ReadModal( ) 

else

f.Open( ) 

endif

CLASS tooltestForm OF FORM

with (this)

 onOpen = class::show_toolbar

 height = 8.6471

 left = 3.625

 top = 1.7059

 width = 23.75

 text = ""

endwith

this PUSHBUTTON = new PUSHBUTTON1(this)

with (this.PUSHBUTTON1)

 onClick = class::show_toolbar

 height = 1.1176

 left = 4

 top = 2

 width = 15.875

 text = "PUSHBUTTON1"

 metric = 0

 fontBold = false

 group = true

endwith

// {Linked Method} Form.onOpen

function Form_onOpen

// {Linked Method} Form.pushbutton1.onClick

function PUSHBUTTON1_onClick

function show_toolbar

 t = findinstance( "myTBar" )

 if empty( t )

  ? "Creating toolbar"

  t = new myTBar( )

 endif

 try

  t.attach( form )

 catch ( Exception e )

  // Ignore already attached error

  ? "Already attached"

 endtry

ENDCLASS

class myTBar of toolbar

 this.imagewidth = 16

 this.flat = true

 this.floating = false

 this.b1 = new toolbutton(this)

 this.b1.bitmap = 'filename ..\artwork\button\dooropen.bmp'

 this.b1.onClick = {;msgbox("door is open")}

 this.b1.speedtip = 'button1'

 this.b2 = new toolbutton(this)

 this.b2.bitmap = 'filename ..\artwork\button\doorshut.bmp'

 this.b2.onClick = {;msgbox("door is shut")}

 this.b2.speedtip = 'button2'

endclass

Note that the only change to the contents of the earlier program is the removal of the FormObj parameter definition (and related change to the referenced form object, to form, in the new method called show_bar)and the removal of the unneeded pcount( ) parameter check at the top of the file.

Otherwise, the code was simply partitioned and placed in the appropriate areas of the form source, and the new method, show_bar, was created to hold the instance-checking and toolbar creation/attachment code.