How to: Retrieve Information from the Parent Form of a Dialog Box

Depending on what you need to do with your dialog box, you might want to have access to information provided by the dialog box's parent form. This information might be required for initialization of the dialog box or involve specific details about the application state of the parent form.

To access public data from a parent form

  1. Open the code for your form.

  2. At the appropriate location within your code, use the Form.ParentForm property of the dialog box to access the public members of the parent form. You should explicitly convert the reference returned by the ParentForm property to the appropriate type.

    The following code demonstrates using the ParentForm property to access a property (in this example, the Text property) on the parent form:

    Public Sub GetParentText()
       Dim x as String
       x = CType(Me.ParentForm, Form1).Text
    End Sub
    
    public void GetParentText()
    {
       string x = ((Form1)this.ParentForm).Text;
    }
    
    public void GetParentText()
    {
       String x = new String((this.get_ParentForm().get_Text()));
    }
    
    public:
       void GetParentText()
       {
          String^ x = (safe_cast<Form1^>(this->ParentForm))->Text;
       }
    

See Also

Tasks

How to: Create Dialog Boxes at Design Time

How to: Retrieve the Result for Dialog Boxes

Reference

ParentForm

Other Resources

Dialog Boxes in Windows Forms