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:
//C# example
private void
button1_Click(object sender,
EventArgs e)
{
Form2 f = new Form2();
f.ShowDialog(this);
}
Dim f As New Form2
f.ShowDialog(Me)
End Sub
In Form2, the code can now refer back to the calling form Form1 by using the “Owner” property.
//C#
example
private void
button1_Click(object sender,
EventArgs e)
{
MessageBox.Show("Parent form text: " + ((Form1)this.Owner).Text);
}MessageBox.Show("Parent form text: " + CType(Me.Owner, Form1).Text)
End Sub
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.
Thank you! Thank you! I just finished reading this document, which was part of a link in the recent Buzz newsletter. I have printed it for others to read, especially those skeptical on the powers of Access and its capabilities.
Darren D.
All Our Microsoft Access Products