Passing a parameter to a custom property of a VFP form
It is easy to pass a parameter into a Visual FoxPro form, the syntax is almost exactly
the same as when you are passing a parameter into a procedure:
Do
myProc
With
myParameter
Do Form
myForm
With
myParameter
The only difficulty arises when you are receiving the parameter into the form.
As with a procedure, you need a Lparameter statement to
declare the parameter. The declaration has to go into the
Init method of the form and this is where confusion can
arise because the Load event fires before
Init and might seem to be the more natural place to
declare the parameters.
Having declared the parameters in the right method, the next difficulty is that the
parameters are local to this method and won't be available anywhere else in the form.
Sometimes this doesn't matter. If the parameter is only being used to customise the
appearance of the form by setting a label or caption then this can be done in the
Init method - remember that the form's
Init does not fire until all the controls on the form have
been created so the label will exist at this point and we would be able to set its
caption.
More often though we'll be wanting to use the parameters sometime later in the lifetime
of the form so we need to store them somewhere where they will remain available. One
way of doing this would be to create an invisible label on the form and to hold the
value here. You might have to do this in Access but Fox allows us to create a custom
property of the form and this can be used to hold the parameter's value for the
lifetime of the form.
Create a custom property
Select from the
menu. This will open the New Property dialog as
shown below. In this example I've set three attributes of the property:
-
nOrderNo as the name of the property. This is following the VFP convention that the
names of numeric variables start with "n".
-
Zero as the initial value of the property. Without this, the property would be
created with a default value of .F. and we might get a
data type mismatch if we tried to use the property before it had been initialised.
-
"Order number as an integer" as the description. This field is optional but the
description shows up in the Property Window of the Form Designer as shown below
and is very useful when you come back to do maintenance work.
Click the button to add this property to the form
then .
I've left the and
boxes unticked on the dialog. These would
create methods that would run whenever the property was read or written to. In strict
object-oriented terms the Access and Assign methods isolate the properties of the object
and give us complete control over how these properties are available to other objects in
the application. These methods aren't needed in this simple example.
The new property appears under the tab of the Visual
FoxPro Properties window. Note that the property name is always shown in lower case
regardless of how the name was originally entered. This is deliberate because properties
are displayed in alphabetical order and a lower-case name forces the custom properties to
the bottom of the list where they are easier to find.
If you need to change the property then right-click and select
from the shortcut menu or select
from the
menu. Neither of these techniques will let you
change the initial value of the property. Deleting and recreating the property seems
to be the only way of doing this.
Storing the parameter on the form
Now that the property has been created, the parameter can be stored by a simple
assignment in the method of the form:
Lparameters tnOrderNo
This.nOrderNo = tnOrderNo
|