How to Allow a Dialog Form to Retrieve Information from its
Provided by: Jim Ferguson, Senior Application Architect
There are several ways to pass information to a custom dialog form from an
owner form. You can set custom properties of the dialog before it is shown,
or pass information to the dialog form in its constructor. When the custom
dialog box is closed, the caller can then refer to the properties of the
dialog box and the DialogResult that indicates whether the user chose to
Accept or Cancel an operation.
But what if the dialog box needs to refer to properties of the calling form?
In addition to having the caller pass in information to the dialog box
before it is shown by setting properties or specifying constructor
parameters, the dialog box can obtain a reference to the calling form and
“pull” information from the caller rather than having the caller “push”
information to the dialog box. This may be convenient if there are many
properties of the caller that need to be referenced.
In order to pass a reference to the caller form to the dialog box, use the
overloaded ShowDialog method that sends an instance of the caller to the
dialog. Assuming a caller named “Form1” which is calling a dialog form named
“Form2” the code would be like so:
Form2 f = new
ByVal e As System.EventArgs)
Dim f As
In Form2, the code can now refer back to the calling form Form1 by using the
form text: " + ((Form1)this.Owner).Text);
ByVal e As
MessageBox.Show("Parent form text: " +
In this example we simply refer to the title bar text of the calling form,
but you could refer to any other properties including custom properties.
Notice that the Owner property is the base “Form” type. You must cast the
Owner property to a specific form type in order to refer to custom
properties that are not in the base Form class.
Return to the tips page