Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Beginner
Aug 28, 1998

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	' modal
End 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 txtAmount
End Sub

Sub CalculatorDialog_Show (C As Control)
'' Display the Calculator_Dialog and return 
'' the calculated value as a string
Load Calculator_Dialog
Calculator_Dialog.lblReadout = _
	Format(Val(C.Text), "#.###############") 
' Pass the number to dialog
Calculator_Dialog.Op1 = Format(Val(C.Text), _
' simulate user input
Calculator_Dialog.LastInput = "NUMS"
Calculator_Dialog.Show 1	' modal
If Calculator_Dialog.Action = True Then
	C.Text = Format(Calculator_Dialog._
		lblReadout, "General Number")
End If
Unload Calculator_Dialog
Set Calculator_Dialog = Nothing
End 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.

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.

Rob Parsons,
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date