Using Toolbars in Visual FoxPro
Using Toolbars in Visual FoxPro
7/9/13
want to have a "Next" menu item to do the same thing. Clearly there are some problems with this approach. A much better approach, used by nearly all popular application frameworks, is to delegate responsibility for toolbar actions to the target forms themselves. In this case, the toolbar button's Click() event code will simply call the active form's Next() method, and the form can handle it however it sees fit. Now you have a very simple Next button that will always work for any form that has a Next() method. The Click() event code for a simple Next button using this technique might look like the following: * Check if there is an active form, and use PEMSTATUS() to see if it has a Next() method IF TYPE('_SCREEN.activeform.name') = 'C' AND PEMSTATUS(_SCREEN.activeform, 'Next', 5) _SCREEN.ActiveForm.Next() ENDIF
Formsets
One way is to create a form-set, and add the toolbar along with the associated forms to the formset. The toolbar will then be created whenever you open the formset, along with all the forms. You can then add code to the toolbar buttons to take appropriate actions when they are pushed. This approach is similar to the old Foundation READ examples provided with FPW 2.6. While you might be tempted to take this approach at first, there are a few problems with it. The toolbar is limited to only the forms in the formset, and each formset you make will need its own toolbar. If you allow more than one formset open at once, you get multiple toolbars - very confusing. All forms in a formset are created at one time, along with the toolbar, so it is very slow to start up. You can't create the toolbar without the forms, so buttons like "Print Reports" or "Exit" would not be available until you open all the forms associated with the toolbar. For these reasons, using a formset to integrate toolbars and forms is usually not a very good approach.
Standalone Toolbars
The preferred technique is to create standalone toolbars that are independent from any particular form. You can define standalone toolbar classes using the VFP Class Designer, and then create them at runtime using the CREATEOBJECT() function. Typically an application will open one or more toolbars when it starts up, and they will remain open for the life of the application session. More advanced frameworks will allow the user to interactively open and close toolbars with some type of "View Toolbars" menu option. With this approach, we can have any number of toolbars open at any given time, you can use them before any forms are created, and every form in the application can easily share the same toolbar. But now there are a few other small problems that turn up. How can the forms find references to the toolbars to refresh them when needed? And what happens if we want to add menu items to do the same thing as some of the toolbar buttons? The menu items will need to enable and disable at the proper times as well as the toolbars. There are many possible ways to solve those problems. One of the simplest, most elegant, and most flexible solutions is to use a global State Manager object to act as a mediator between menus, toolbars, forms, and any other service objects in your application.
www.dfpug.de/loseblattsammlung/migration/whitepapers/TOOLBARS.htm 2/4
7/9/13
7/9/13
ENDIF ENDFOR ENDFUNC The RefreshToolbars() method provides an easy way for forms to refresh all the toolbars whenever needed. Adding this method to the state manager means your forms don't need to bother keeping track of which toolbars are currently open. Normally, a form should call goStatemanager.RefreshToolbars() whenever the form is refreshed, when it becomes active, when it is closed, and any other time something happens they may affect toolbar button settings.
Summary
A state manager object takes some extra work up front to design and create, but once you have one in place, it makes integrating forms, toolbars and menus much easier. Some of the many benefits include: Your forms don't need to know anything about what toolbars or menus are available at runtime. Any form can use any toolbar. Your code doesn't depend on any particular menu or toolbar layout. You can allow users to open or close toolbars interactively, however they like. Menu items and toolbar buttons are always kept in sync, without any additional effort on your part. The State Manager object included with the Codemine Development System Framework includes many additional features for even greater flexibility. Most notably, it allows for automatic background refreshes of toolbars during system idle time whenever the active window changes. Using idle time to refresh toolbars improves the performance of your application, and also reduces the number of places your forms need to explicitly request toolbar refreshes. For more information and a sample application, visit the Soft Classics web site at www.Codemine.com Author Info: Dave Lehr Soft Classics, Ltd. Phone: 207-942-4112 Fax: 207-942-3693 email: [email protected]
www.dfpug.de/loseblattsammlung/migration/whitepapers/TOOLBARS.htm
4/4