devxlogo

Pass Parameters to User Dialogs

Pass Parameters to User Dialogs

It’s fairly common practice to add buttons to your date and numeric fields to allow users to select and display a popup calendar or calculator. However, to display a calculator or calendar without allowing it to communicate with the calling application defeats the purpose of giving the user these capabilities. The called dialog is only an appendage that cannot pass the user’s calculation back to the field from which the resource was called.

To solve this problem, add a button to a form next to a numeric input field (txtAmount). In the Click event of this button, place a Load statement for the form you want to show the user:

 Sub cmd_Click()	Load Calculator_Dialog	Calculator_Dialog.Show 1	' modalEnd Sub

Pass parameters to a form by writing a wrapper utility that handles the passing of these parameters to and from the called dialog:

 Sub cmd_Click()	CalculatorDialog_Show txtAmountEnd SubSub CalculatorDialog_Show (C As Control)'' Display the Calculator_Dialog and return '' the calculated value as a stringLoad Calculator_DialogCalculator_Dialog.lblReadout = _	Format(Val(C.Text), "#.###############") ' Pass the number to dialogCalculator_Dialog.Op1 = Format(Val(C.Text), _	"#.###############")' simulate user inputCalculator_Dialog.LastInput = "NUMS"Calculator_Dialog.Show 1	' modalIf Calculator_Dialog.Action = True Then	C.Text = Format(Calculator_Dialog._		lblReadout, "General Number")End IfUnload Calculator_DialogSet Calculator_Dialog = NothingEnd Sub

The parameters are passed to hidden label controls on the Calculator_Dialog form between the time the form is loaded and the time it’s displayed. A hidden label control on the Calculator_Dialog (Action) indicates whether the user chose the OK or Cancel button to exit the dialog. Rather than unload the Calculator_Dialog when the user presses the OK or Cancel button, the form hides until the calling sub (CalculatorDialog_Show) processes the value of Calculator_Dialog.lblReadout. The form is then unloaded, and the Set-to-Nothing statement is executed to purge memory of the form’s controls and procedures.

See also  Why ChatGPT Is So Important Today

This technique works with any version of VB. Starting with VB4, you can add properties and methods to forms, which is a preferable method to relying on hidden labels for interobject communication.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist