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 |
---|---|
|
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 |
---|---|
|
Start of the class definition: one toolbar with a command button, a separator, and another command button. |
|
Sets properties of the toolbar object. |
|
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. |
|
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 . |
|
When the user clicks cmdBold , the FontBold setting of frmForm1 is reversed, and the FontBold setting of cmdBold is set to match it. |
|
When the user clicks cmdItalic , the FontItalic setting of frmForm1 is reversed, and the FontItalic setting of cmdItalic is set to match it. |
|
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 | Configuring Visual FoxPro | Defining Toolbar Actions