Control.Location 属性

定义

获取或设置该控件的左上角相对于其容器的左上角的坐标。Gets or sets the coordinates of the upper-left corner of the control relative to the upper-left corner of its container.

public:
 property System::Drawing::Point Location { System::Drawing::Point get(); void set(System::Drawing::Point value); };
public System.Drawing.Point Location { get; set; }
member this.Location : System.Drawing.Point with get, set
Public Property Location As Point

属性值

Point,表示控件的左上角相对于其容器的左上角。The Point that represents the upper-left corner of the control relative to the upper-left corner of its container.

示例

下面的代码示例创建 GroupBox,并设置其一些公共属性。The following code example creates a GroupBox and sets some of its common properties. 该示例创建一个 TextBox 并在 "分组" 框中设置其 LocationThe example creates a TextBox and sets its Location within the group box. 接下来,它设置分组框的 Text 属性,并将组框停靠到窗体的顶部。Next, it sets the Text property of the group box, and docks the group box to the top of the form. 最后,它通过将 Enabled 属性设置为 false 来禁用分组框,这将导致禁用组中包含的所有控件。Lastly, it disables the group box by setting the Enabled property to false, which causes all controls contained within the group box to be disabled.

   // Add a GroupBox to a form and set some of its common properties.
private:
   void AddMyGroupBox()
   {
      // Create a GroupBox and add a TextBox to it.
      GroupBox^ groupBox1 = gcnew GroupBox;
      TextBox^ textBox1 = gcnew TextBox;
      textBox1->Location = Point(15,15);
      groupBox1->Controls->Add( textBox1 );

      // Set the Text and Dock properties of the GroupBox.
      groupBox1->Text = "MyGroupBox";
      groupBox1->Dock = DockStyle::Top;

      // Disable the GroupBox (which disables all its child controls)
      groupBox1->Enabled = false;

      // Add the Groupbox to the form.
      this->Controls->Add( groupBox1 );
   }
// Add a GroupBox to a form and set some of its common properties.
private void AddMyGroupBox()
{
   // Create a GroupBox and add a TextBox to it.
   GroupBox groupBox1 = new GroupBox();
   TextBox textBox1 = new TextBox();
   textBox1.Location = new Point(15, 15);
   groupBox1.Controls.Add(textBox1);

   // Set the Text and Dock properties of the GroupBox.
   groupBox1.Text = "MyGroupBox";
   groupBox1.Dock = DockStyle.Top;

   // Disable the GroupBox (which disables all its child controls)
   groupBox1.Enabled = false;

   // Add the Groupbox to the form.
   this.Controls.Add(groupBox1);
}
' Add a GroupBox to a form and set some of its common properties.
Private Sub AddMyGroupBox()
   ' Create a GroupBox and add a TextBox to it.
   Dim groupBox1 As New GroupBox()
   Dim textBox1 As New TextBox()
   textBox1.Location = New Point(15, 15)
   groupBox1.Controls.Add(textBox1)
   
   ' Set the Text and Dock properties of the GroupBox.
   groupBox1.Text = "MyGroupBox"
   groupBox1.Dock = DockStyle.Top
   
   ' Disable the GroupBox (which disables all its child controls)
   groupBox1.Enabled = False
   
   ' Add the Groupbox to the form.
   Me.Controls.Add(groupBox1)
End Sub

注解

由于 Point 类是值类型(在 Visual Basic 中Structurestruct 在视觉对象C#中),因此它按值返回,这意味着访问属性将返回控件左上角的副本。Because the Point class is a value type (Structure in Visual Basic, struct in Visual C#), it is returned by value, meaning accessing the property returns a copy of the upper-left point of the control. 因此,调整从此属性返回的 PointXY 属性将不会影响该控件的 LeftRightTopBottom 属性值。So, adjusting the X or Y properties of the Point returned from this property will not affect the Left, Right, Top, or Bottom property values of the control. 若要调整这些属性,请分别设置每个属性值,或使用新的 Point设置 Location 属性。To adjust these properties set each property value individually, or set the Location property with a new Point.

如果 ControlForm,则 Location 属性值表示屏幕坐标中的 Form 的左上角。If the Control is a Form, the Location property value represents the upper-left corner of the Form in screen coordinates.

适用于

另请参阅