Partager via


Coordinating Toolbars and Forms Using Code

In addition to using the Form Designer, you can add toolbars to form sets by using code.

To add a toolbar to a form set using code

  • In the form set's Init event, use the SET CLASSLIB command to specify the library containing the toolbar class, and then create a toolbar from that class in the form set.

For example, to add and display the toolbar tbrPrint, which is based on the printing class in the inventory class library, add the following code to the form set's Init event:

SET CLASSLIB TO inventory
THIS.AddObject("tbrPrint","printing")
THIS.tbrPrint.Show

Note   If the toolbar class does not define the actions of the toolbar and its buttons, you must define the actions in the event procedures associated with the toolbar and its buttons.

Example:

You can define all aspects of a toolbar in code. For example, if you add the following code to a form set's Init event, when the form set is loaded Visual FoxPro creates and displays the toolbar defined in the code. This toolbar contains two buttons.

When chosen, these buttons change the font attributes of the form frmForm1 in the form set.

Form Set Init Event Code

Code Comments
THIS.AddObject("tbrTool1","mytoolbar")
THIS.tbrTool1.Show
Adds a toolbar of the class mytoolbar to the current form set and makes the toolbar visible. This code is in the form set's Init event.

Class definition code

Code Comments
DEFINE CLASS myToolBar AS TOOLBAR
ADD OBJECT cmdBold AS COMMANDBUTTON
ADD OBJECT sep1    AS SEPARATOR
ADD OBJECT cmdItalic AS COMMANDBUTTON
Start of the class definition: one toolbar with a command button, a separator, and another command button.
Left = 1
Top  = 1
Width = 25
Caption = "Form Attributes"
Sets properties of the toolbar object.
cmdBold.Caption = "B"
cmdBold.Height = 1.7
cmdBold.Width = 10
cmdItalic.Caption = "I"
cmdItalic.Height = 1.7
cmdItalic.Width = 10
cmdItalic.FontBold = .F.
Sets properties of the controls. Notice that there are no Top or Left property settings for controls on a toolbar. Controls on a toolbar are automatically positioned in the order they are added.

The FontBold property of cmdItalic is set to false (.F.) because FontBold is true (.T.) by default.
PROCEDURE Activate
   THIS.cmdBold.FontBold = ;
    THISFORMSET.frmForm1.FontBold
   THIS.cmdItalic.FontItalic = ;
    THISFORMSET.frmForm1.FontItalic
ENDPROC
When the toolbar is activated, the font attributes of the two command buttons are set to reflect the Bold and Italic font settings of frmForm1.
PROCEDURE cmdBold.CLICK
   THISFORMSET.frmForm1.FontBold = ;
    !THISFORMSET.frmForm1.FontBold
   THIS.FontBold = ;
    THISFORMSET.frmForm1.FontBold
ENDPROC
When the user clicks cmdBold, the FontBold setting of frmForm1 is reversed, and the FontBold setting of cmdBold is set to match it.
PROCEDURE cmdItalic.CLICK
   THISFORMSET.frmForm1.FontItalic = ;
    !THISFORMSET.frmForm1.FontItalic
   THIS.FontItalic = ;
    THISFORMSET.frmForm1.FontItalic
ENDPROC
When the user clicks cmdItalic, the FontItalic setting of frmForm1 is reversed, and the FontItalic setting of cmdItalic is set to match it.
ENDDEFINE
End of the class definition.

Setting Properties of Custom Toolbars

While designing a custom toolbar, you can set its properties. For example, you can set the Movable property to allow the user to move the toolbar.

Additionally, you can use methods and events to control custom toolbars. For example, you can use the Dock method to dock or float a toolbar, and you can use the BeforeDock event and AfterDock event to control what happens before and after a toolbar is docked.

See Also

Adding Custom Toolbars to Form Sets | Defining Toolbar Actions | Creating Custom Toolbars | Customize Toolbar Dialog Box | Designing Menus and Toolbars | Customizing the Visual FoxPro Environment | Defining Toolbar Actions