ContainerControl.ParentForm 属性

定义

获取将容器控件分配给的窗体。

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

属性值

向其分配容器控件的 Form。 如果控件承载于 Internet Explorer 内部或者另一个没有父窗体的宿主上下文中,则该属性将返回 null。

属性

示例

下面的代码示例演示如何创建两个窗体: Form1Form2。 将 IsMdiContainer 的 属性设置为 true ,并将其MdiParent设置为 的 Form2Form1 。 接下来,在每个窗体上创建一个按钮 button1。 单击父窗体上的按钮时,事件处理程序将显示子窗体。 单击子窗体上的 按钮时,事件处理程序将显示 Name 其父窗体的 属性。 使用以下两个代码段覆盖 button1 这两种形式的事件处理程序。

   // 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

适用于

另请参阅