ContainerControl.ParentForm 属性

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

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public ReadOnly Property ParentForm As Form
用法
Dim instance As ContainerControl
Dim value As Form

value = instance.ParentForm
public Form ParentForm { get; }
public:
property Form^ ParentForm {
    Form^ get ();
}
/** @property */
public Form get_ParentForm ()
public function get ParentForm () : Form

属性值

将容器控件分配给的 Form

示例

下面的代码示例演示如何创建两个窗体:Form1Form2。将 Form1IsMdiContainer 属性设置为 true,并使其作为 Form2MdiParent。接下来,在每个窗体上创建一个按钮 button1。当单击父窗体上的按钮时,事件处理程序显示子窗体。当单击子窗体上的按钮时,事件处理程序显示其父窗体的 Name 属性。使用下面的两个代码段重写两个窗体中的 button1 事件处理程序。

' 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 Form1()
    ' Make this form the parent of f2.
    f2.MdiParent = Me
    ' Display the form.
    f2.Show()
End Sub 'button1_Click
// The event handler on Form1.
private void button1_Click(object sender, System.EventArgs e)
{
    // Create an instance of Form2.
    Form1 f2 = new Form1();
    // 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.
      Form1^ f2 = gcnew Form1;

      // 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.
    Form1 f2 = new Form1();
    // Make this form the parent of f2.
    f2.set_MdiParent(this);
    // Display the form.
    f2.Show();
} //button1_Click
' 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 'button1_Click
// 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:
   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 = get_ParentForm().get_Name();
    // Display the name in a message box.
    MessageBox.Show("My Parent is " + s + ".");
} //button1_Click

.NET Framework 安全性

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

ContainerControl 类
ContainerControl 成员
System.Windows.Forms 命名空间
Form