|How to assign code to event in run-time?|
In VFP there are no direct way to assign code to the method or event in run-time mode. However, some workaround is possible with use of additional, 'hook' class. The sample below describes the approach. It is possible to make it generic. The approach is very useful to catch, for example, events of the _Screen form.
DEFINE CLASS MyHook
oHookedObject = eval(m.lcToBeHooked)
&& your code here
ENDDEFINEThan add this object to the form or the container that require to use it:
m.lcToBeHooked = thisform.MyObjectRequiredToeHooked
set procedure to hook.PRG
&& after this moment the oHookedObject.Valid code
&& is called for Valid event of the MyObjectRequiredToeHooked
endwithYou can build a hook.PRG file on the fly with the code you require in a string, save to file, set procedure to it, create an object. But do not release PRG from memory until you destroy the form/container having hook object. So when you need this quite universal, you have a good reason to track all hooks or even build hooks manager class for these purposes that will store all hooks and release the when they're free.
The approach is not tested in the live environment, though it works very well for a single use in such cases as assigning code to the _Screen form events. In addition, defined by this way _Assign or _Access methods for properties do not catch the assignment/access for that property, if these methods are not already defined.
For ActiveX objects you require to use the VFPCOM.DLL to bind VFP code to the ActiveX object event in VFP6, in VFP7 you can use EventHandler() function. Described above approach works with VFP native objects only.
(UPDATED) There are also reports that assigning code this way to OLE* methods for OLE drag and drop does not work.
(UPDATED2) There is also a problem met when assigning the code this way to the grid elements, specially to columns. Its hard to get rid of crashes, and form do not want to close till explicit destroying of all Hook objects before form Release method calling or in QueryUnload event. After all, some forms might cause occasional crashes with such hooks (VFP6 SP5).
More entries from this member
- 256-color icons in EXE application compiled by VFP May 28, 2002
VFP does not support 256-color icons as project's icon (icon displayed for EXE in Windows Explorer). But 16-color icons usually looks ugly and too simplified.
Here is an approach that allows organize 256-color icons for VFP EXE application.
The approach is based on use of Resource Hacker application. You can download it from following URL:
(UPDATED: original link does not work for some reason. Let me know if this one also does not ...
- Force positioning of text cursor by right mouse click in EditBox and RTF ActiveX controls February 8, 2002
There is a situation often when we need some way to position a text cursor in the text of Editbox or RTF ActiveX controlwhen right click on the control. For example, we want to display a shortcut menu on right click. In menu, user select an option that insert some text into the control on place of current position. However, right click on the control does not change the text cursor position, that confuse users. It would be good to right click, cursor moves to the clicked point and then menu disp...
- Grid reconstruction and how to avoid it March 16, 2001
Have you ever found a situation when your grid don't want to behave as you directed in design time? Custom controls in columns lost? Code of columns, headers or controls event not running? Read this FAQ then.
Grid reconstruction behavior is a complete removing of all grid's controls and columns and creating them again using default VFP controls. This cause lose of all methods, property settings and objects in columns and all column objects, column control to display and edit data is reset to ...
- How to display Tool Tips for different parts of the Grid? February 1, 2002
In VFP6 Grid value of the ToolTipText property of controls is displayed only for the entire grid or for current control when mouse is over it. Becuase there are a lot of parts in the grid, it is often useful to display tool tip for different parts separately. Unfortunately, VFP interprets grid as a single control, displayed tool tip appearance for grid is not changed immediately even when ToolTipText property is changed, until mouse is moved out of the grid and returned into the grid into requir...
- How to freeze (lock) a column in the grid without use of split bar February 8, 2002
The approach is based on a simple principle. LeftColumn property of the grid represents a column that is currently the leftmost visible column in the grid with current horizontal scrolling. As we scroll the grid horizontally, this property changes. We can assign this number to the ColumnOrder property of the column we want to hold and that’s all:
this.Columns(1).ColumnOrder = this.LeftColumn
You can use this in the Scrolled and AfterRowColChange events of the grid. However, there is a...
- How to get form caption font properties October 6, 2000
Windows form caption properties stored in the user preferences. You can change them using Windows Control Panel\Display\Appearance. To get these parameters programmatically in VFP, you can use following routine as a sample.
- How to make a collection property of VFP object February 6, 2001
After some investigation, I managed to make a collection-like property of object. The following is a simple sample of how to organize collection-like access (rough, need to improve it to work as real collection). It is just to show an approach.
- How to make a grid appear like a list similar to list box June 20, 2001
This article describes how to configure grid to appear as a list similar to the list box or the drop-down list of combo box. It is for appearance only. All other things are as usual for any grid.
This is useful for case when need to replace list box by a grid to display more records, because listbox is slow when displaying 1000+ rows in listbox.
- How to make SQL Pass-Through cursor updatable April 9, 2002
The SQLEXEC() function in VFP allows downloading of data from any data source through ODBC. Unlike remote views, SQL Pass-Through approach does not provide default (automatic) settings in the returned cursor to update data on the server after changes in the cursor at the client side. Anyway, it is possible to customize required properties to organize updating.
First of all, it is recommended to read chapters about SQL Pass Through commands in the MSDN, specially, chapters that explain propert...
- How to organize SUM() for character field in SQL Server SELECT query September 4, 2001
There is a little problem for query to get something like a sum() for a character field to concatenate character values from all records in the group. Here is described solution.
The approach below have an limitation to max 30 records in group for concatenation. Test it if it will work for more records in group or there should be another limitation on your server, becaus ethis number is depended on SQL Server capabilities and length of the character field being concatenated.
- How to put grid on the form programmatically February 5, 2001
Suggest Grid1 is a name of grid required to put onto the form, grid should contain 10 columns an display data from the MyAlias alias. Grid should contain a combo box in the second colunm with the name Combo1. Following sample routine will allow you to see a generic idea of how to put grid on the form programmatically and recreate it again when needed. You can make it as a custom method of the form and call it when needed (for example, thisform.MakeGrid or thisform.RefreshGrid).
- How to refresh grid properly after sorting January 26, 2001
I have seen many questions about sorting of a grid by clicking on its header and proper refreshing of grid after sorting. There is a reliable way to sort (index) grid cursor and properly refresh the grid without change of the record number AND with very good looking. I made this in my grid class and tested it already 2 years so far.
- How to work with Image field type on SQL Server (binary data)? January 11, 2001
Text field on SQL Serevr is not good to store binary data because it is not reliable and often SQL Server corrupts binary data in text field. varbinary type could be too short.
There are certain difficulties to use image field on SQL Server to store and read binary data in VFP. Image field on SQL Server mapped to general field in VFP by default, causing some difficulties when using SQL Pass Through functions. The workaround idea is to try to use memo field to update data on SQL server.
- SPT cursor fetching and progress displaying for SPT cursor downloading February 5, 2002
SQL Pass Through commands often can take a lot of time for running and returning the result set to the client. It is often usefult to show a progress bar in case of such lengthy process. Also, FetchAsNeeded option, available for views, could be very useful for SPT cursors as well. However, how we can open SPT cursor and set FetchAsNeeded option for it?
Here is a sample code for opening the cursor in "FetchAsNeeded" mode, as well as a description of approach for organizing the progress bar for...