<< Click to Display Table of Contents >> Example |
The following example demonstrates the basic steps of loading a template, setting the fields, enabling interaction, setting the focus and showing the form in the preview.
We are using the sample DCD "iForm.dcd", which is shipped with VPE and installed in
"<VPE Install Dir>\images\DCD\iForm.dcd".
You will find the complete source code, which demonstrates all the techniques that are explained throughout the following sections of this manual, at:
•C / C++: subdirectory "C\iform.cpp"
•Visual Basic: subdirectory "VB\vpeidemo\iForm.vbp"
•Delphi: subdirectory "Delphi\vpeidemo\iFormPrj.dpr"
The dycodoc DCD file "iForm.dcd":
Note the two Save and Cancel "Buttons" at the bottom of the form. They are synthesized by using a Checkbox and overlaying a Text Object. The Checkboxes and Texts of the synthesized buttons are marked as "Do not include in Prints", so they are only visible in the preview, but they are not printed.
We also used a special naming convention, in order to identify objects by their names:
•The prefix "iff" is used for Interactive Form Fields (e.g. "iffName").
•The prefix "it" is used for Interactive Texts (e.g. "itAddress").
•The prefix "cb" is used for Checkboxes (e.g. "cbIsHungry").
•The prefix "rb" is used for Radiobuttons (e.g. "rbMeat").
The names of Controls are important to identify them later during runtime by code. For example a Control is enabled or disabled - or the focus is set to a Control - by specifiying its name. Furthermore VPE sends events to your application, with informations about several actions that are currently performed by the user regarding a specific control. While processing such an event, you identify the related Control by inquiring its name.
The FieldStudio field definition file "iForm.tpl":
Note that the size information (for example for the Field "Name", the size is 80), has no effect in the current version of VPE. But entering the size when creating an FLD makes sense, because designers, which use the FLD in dycodoc, immediately have an imagination of how huge the content of the Field is and therefore know how to choose the dimensions of any associated Control.
Sample Code for the VPE Control:
' Sets the fields of the Template "iForm.tpl"
' Integer Fields are by default zero, so we do not set them here
Private Sub SetFields(tpl As TVPETemplate)
tpl.SetFieldAsString "iForm", "Name", "Smith"
tpl.SetFieldAsString "iForm", "FirstName", "Walter"
tpl.SetFieldAsString "iForm", "Address", "12 Some Street" + Chr(10) + Chr(13) + "Any State, ST 123456" + Chr(10) + Chr(13) + "USA" |
tpl.SetFieldAsInteger "iForm", "PreferredFood", 1
End Sub
' Main Program - Create the VPE Interactive Form
' Open the VPE document
Doc.OpenDoc
' Load the template
Set tpl = Doc.LoadTemplate("..\..\images\dcd\iForm.tpl")
' Set the fields
SetFields tpl
' insert the template into the VPE Document
Doc.DumpTemplate tpl
' Enable the whole form for user interaction
' (by default user interaction is always disabled)
Doc.Interaction = True
' show the preview
Doc.Preview
' Set the focus to the first control
' (i.e. the control with the lowest Tab Index)
Doc.SetFocusToFirstControl
Sample Code for the VPE DLL (C/C++):
// Sets the fields of the Template "iForm.tpl"
// Integer Fields are by default zero, so we do not set them here
void SetFields(VpeHandle hTpl)
{
VpeSetTplFieldAsString (hTpl, "iForm", "Name", "Smith");
VpeSetTplFieldAsString (hTpl, "iForm", "FirstName", "Walter");
VpeSetTplFieldAsString (hTpl, "iForm", "Address", "12 Some Street\r\nAny State, ST 123456\r\nUSA"); |
VpeSetTplFieldAsInteger(hTpl, "iForm", "PreferredFood", 1);
}
// Create the Form
void CreateForm()
{
// Open the VPE document
VpeHandle hDoc = VpeOpenDoc(hMainWindow, "Template Usage Demo", VPE_EMBEDDED);
// Load the template
hTplForm = VpeLoadTemplate(hDoc, "..\\images\\dcd\\iForm.tpl");
if (hTplForm)
{
// Set the fields
SetFields(hTplForm);
// insert the template into the VPE Document
VpeDumpTemplate(hDoc, hTplForm);
// Enable the whole form for user interaction
//(by default user interaction is always disabled)
VpeEnableInteraction(hDoc, TRUE);
// show the preview
hDocForm = hDoc;
VpePreviewDoc(hDoc, NULL, VPE_SHOW_MAXIMIZED);
// Set the focus to the first control
// (i.e. the control with the lowest Tab Index)
VpeSetFocusToFirstControl(hDoc);
}
else
{
Msg("Template file could not be loaded! (file not found or wrong version / edition)");
VpeCloseDoc(hDoc);
}
}
The resulting VPE Preview will look like this:
For an explanation on how to work with the preview, please refer to the section “Preview”.