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 中為Structure,在C#Visual 中則為 struct),它是以傳值方式傳回,這表示存取屬性會傳回控制項左上角的複本。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.

如果 ControlFormLocation 屬性值就會以螢幕座標表示 Form 的左上角。If the Control is a Form, the Location property value represents the upper-left corner of the Form in screen coordinates.

適用於

另請參閱