ContainerControl.ParentForm ContainerControl.ParentForm ContainerControl.ParentForm ContainerControl.ParentForm Property

定义

获取将容器控件分配给的窗体。Gets the form that the container control is assigned to.

public:
 property System::Windows::Forms::Form ^ ParentForm { System::Windows::Forms::Form ^ get(); };
[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.Form ParentForm { get; }
member this.ParentForm : System.Windows.Forms.Form
Public ReadOnly Property ParentForm As Form

属性值

将容器控件分配给的 FormThe Form that the container control is assigned to. 如果控件承载于 Internet Explorer 内部或者另一个没有父窗体的宿主上下文中,则该属性将返回 null。This property will return null if the control is hosted inside of Internet Explorer or in another hosting context where there is no parent form.

示例

下面的代码示例演示如何创建两种形式: Form1Form2The following code example shows how to create two forms: Form1 and Form2. Form2将的true 属性设置MdiParent为,并使其成为的。 Form1 IsMdiContainerSet the IsMdiContainer property of Form1 to true and make it the MdiParent of Form2. 接下来,在每个button1窗体上创建一个按钮。Next, create a button, button1, on each form. 单击父窗体上的按钮时,事件处理程序会显示子窗体。When the button on the parent form is clicked, the event handler displays the child form. 单击子窗体上的按钮时,事件处理程序会显示其Name父窗体的属性。When the button on the child form is clicked, the event handler displays the Name property of its parent form. 使用以下两个代码段覆盖button1两种形式的事件处理程序。Use the following two code segments to overwrite button1 event handlers in both forms.

   // The event handler on Form1.
private:
   void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Create an instance of Form2.
      Form1^ f2 = gcnew Form2;

      // Make this form the parent of f2.
      f2->MdiParent = this;

      // Display the form.
      f2->Show();
   }
// The event handler on Form1.
private void button1_Click(object sender, System.EventArgs e)
{
    // Create an instance of Form2.
    Form2 f2 = new Form2();
    // Make this form the parent of f2.
    f2.MdiParent = this;
    // Display the form.
    f2.Show();
}
' The event handler on Form1.
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    ' Create an instance of Form2.
    Dim f2 As New Form2()
    ' Make this form the parent of f2.
    f2.MdiParent = Me
    ' Display the form.
    f2.Show()
End Sub
   // The event handler on Form2.
private:
   void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Get the Name property of the Parent.
      String^ s = ParentForm->Name;

      // Display the name in a message box.
      MessageBox::Show( String::Concat( "My Parent is ", s, "." ) );
   }
// The event handler on Form2.
private void button1_Click(object sender, System.EventArgs e)
{
    // Get the Name property of the Parent.
    string s = ParentForm.Name;
    // Display the name in a message box.
    MessageBox.Show("My Parent is " + s + ".");
}
' The event handler on Form2.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ' Get the Name property of the parent.
    Dim s As String = ParentForm.Name
    ' Display the name in a message box.
    MessageBox.Show("My parent is " + s + ".")
End Sub

安全性

UIPermission
对于所有窗口,获取此属性值。for all windows to get this property value. 关联的枚举:AllWindowsAssociated enumeration: AllWindows

适用于

另请参阅