Passing Parameters to Forms

One form opens another form - now you want to pass a few parameters each way.

For instance, you open a dialog box to allow the user to set a few parameters

This is a trivial task, all programs need to accomplish this, so why aren't any of the techniques documented?

I discuss several ways to do this under Application Parameters, data can also be passed via a database.

This page addresses directly reading and writing data from one form to another. CustomDialogBoxes.html provides several ways to directly read and write data from one form to another in MS Access and other VisualBasic based languages. In particular, the Form.Tag technique is applicable to most windows languages.

Delphi


Delphi 5.0

There are 2 basic methods to open a second form One method to pass parameters is to use a callback - after the second form is displayed, set form2.OnClose (or form2.OnHide) to point to a method on the first form. Then, when the second form is closed (or hidden), the callback method will be called and it can read the various parameters

If all the parameters are stored in an ini-class, you could just pass a pointer to that class and let the second form handle reading and writing the parameters. A callback method might still be used to tell the original form that new parameters are available.

You could pass a single integer parameter via the form's Tag parameter. This is a convenient way to indicate which button was pressed on a non-modal dialog box. (MS Access allows the Form.tag parameter to be a string.)


Modal Forms

Modal forms (dialog boxes) suspend operation of the calling form and automatically pass a parameter back to it. This is similar to using the Tag property, except the modal forms are closed by setting the ModalResult property.

This example (based on the Delphi 5 help - TCustomForm.ShowModal example) shows how to

In your dialog box, attatch this code to your buttons. Remeber, simply setting ModalResult hides the dialog box and returns execution to the calling form.


Callback Example

This example is from one of my programs - a non-modal form passes a single string parameter back to the application's main form. The non-modal form is created in the project's *.dpr file and displayed via a menu option. In the main form, this sets the callback and displays the form.


Author: Robert Clemenzi - clemenzi@cpcug.org
URL: http:// cpcug.org / user / clemenzi / technical / Languages / PassingParameters.html