Creating toolbars and attaching them to forms
Note that this topic covers both object creation and attachment. That’s because, like popup menus, you need to add some code to attach toolbars to your forms. However, unlike pulldown or popup menus—which you can create using special visual designers—you also have to define your toolbars programmatically, either in a reusable program or within your form’s code.
Like any other object, toolbar and toolbutton classes have a number of properties that allow you to modify the behavior and appearance of a toolbar. These properties, some of which are illustrated in the following code examples, are described later in this series of Help topics and are covered in detail in the printed and online Language Reference.
Creating a reusable toolbar
Here’s an example of an object definition program, MYTOOLBR.PRG, which defines a basic two-button toolbar for use in any form or application.
parameter FormObj
if pcount( ) < 1
msgbox("DO mytoolbr.prg WITH <form reference>")
return
endif
t = findinstance( "myTBar" )
if empty( t )
? "Creating toolbar"
t = new myTBar( )
endif
try
t.attach( FormObj )
catch ( Exception e )
// Ignore already attached error
? "Already attached"
endtry
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
Attaching a reusable toolbar
As with popup menus, you can attach a reusable toolbar definition file to your forms with a simple DO command. However, since forms don’t have a toolbar property, the connection is defined in the toolbar’s own attach( ) property. Thus, if you choose to connect the program described above through a form’s onOpen event, the integration codeblock is simply this:
{;do mytoolbr.prg with this}
Or, if you prefer the linked method approach, click the onOpen event’s tool button and add the integration code:
// {Linked Method} Form.onOpen
function Form_onOpen
do mytoolbr.prg with this
Of course, you also need to provide a way to restore the toolbar if the user has closed it. You can do that by also adding the integration code (or codeblock) to the onClick event of another control, such as a menu item or button. Should the toolbar already be running when it is summoned, findInstance( ) will let you know and let you block the creation of a new instance.
As is the case with pulldown menus, keep in mind that if your form’s MDI property is set to True, your toolbar is owned by (and may only be docked to) the form’s parent window or application frame.