Control.Parent 属性

获取或设置控件的父容器。

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

语法

声明
Public Property Parent As Control
用法
Dim instance As Control
Dim value As Control

value = instance.Parent

instance.Parent = value
public Control Parent { get; set; }
public:
property Control^ Parent {
    Control^ get ();
    void set (Control^ value);
}
/** @property */
public Control get_Parent ()

/** @property */
public void set_Parent (Control value)
public function get Parent () : Control

public function set Parent (value : Control)

属性值

一个 Control,表示控件的父控件或容器控件。

备注

Parent 属性值设置为 空引用(在 Visual Basic 中为 Nothing) 会从控件的当前父控件的 Control.ControlCollection 中移除控件。

示例

下面的代码示例使用 Parent 属性和 FindForm 方法来设置按钮的父控件及其窗体上的属性。

' This example uses the Parent property and the Find method of Control to set
' properties on the parent control of a Button and its Form. The example assumes
' that a Button control named button1 is located within a GroupBox control. The 
' example also assumes that the Click event of the Button control is connected to
' the event handler method defined in the example.
Private Sub button1_Click(sender As Object, e As System.EventArgs) Handles button1.Click
   ' Get the control the Button control is located in. In this case a GroupBox.
   Dim control As Control = button1.Parent
   ' Set the text and backcolor of the parent control.
   control.Text = "My Groupbox"
   control.BackColor = Color.Blue
   ' Get the form that the Button control is contained within.
   Dim myForm As Form = button1.FindForm()
   ' Set the text and color of the form containing the Button.
   myForm.Text = "The Form of My Control"
   myForm.BackColor = Color.Red
End Sub
// This example uses the Parent property and the Find method of Control to set
// properties on the parent control of a Button and its Form. The example assumes
// that a Button control named button1 is located within a GroupBox control. The 
// example also assumes that the Click event of the Button control is connected to
// the event handler method defined in the example.
private void button1_Click(object sender, System.EventArgs e)
{
   // Get the control the Button control is located in. In this case a GroupBox.
   Control control = button1.Parent;
   // Set the text and backcolor of the parent control.
   control.Text = "My Groupbox";
   control.BackColor = Color.Blue;
   // Get the form that the Button control is contained within.
   Form myForm = button1.FindForm();
   // Set the text and color of the form containing the Button.
   myForm.Text = "The Form of My Control";
   myForm.BackColor = Color.Red;
}
   // This example uses the Parent property and the Find method of Control to set
   // properties on the parent control of a Button and its Form. The example assumes
   // that a Button control named button1 is located within a GroupBox control. The 
   // example also assumes that the Click event of the Button control is connected to
   // the event handler method defined in the example.
private:
   void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Get the control the Button control is located in. In this case a GroupBox.
      Control^ control = button1->Parent;
      
      // Set the text and backcolor of the parent control.
      control->Text = "My Groupbox";
      control->BackColor = Color::Blue;
      
      // Get the form that the Button control is contained within.
      Form^ myForm = button1->FindForm();
      
      // Set the text and color of the form containing the Button.
      myForm->Text = "The Form of My Control";
      myForm->BackColor = Color::Red;
   }
// This example uses the Parent property and the Find method of Control to 
// set properties on the parent control of a Button and its Form. The 
// example assumes that a Button control named button1 is located within a 
// GroupBox control. The example also assumes that the Click event of the 
// Button control is connected to the event handler method defined in the 
// example.
private void button1_Click(Object sender, System.EventArgs e)
{
    // Get the control the Button control is located in. 
    // In this case a GroupBox.
    Control control = button1.get_Parent();

    // Set the text and backcolor of the parent control.
    control.set_Text("My Groupbox");
    control.set_BackColor(Color.get_Blue());

    // Get the form that the Button control is contained within.
    Form myForm = button1.FindForm();

    // Set the text and color of the form containing the Button.
    myForm.set_Text("The Form of My Control");
    myForm.set_BackColor(Color.get_Red());
} //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

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Control 类
Control 成员
System.Windows.Forms 命名空间
Control.Controls 属性
Control.ControlCollection