Control.Anchor Control.Anchor Control.Anchor Control.Anchor Property

Definición

Obtiene o establece los bordes del contenedor al que está enlazado un control y determina cómo se cambia el tamaño de un control con su elemento primario.Gets or sets the edges of the container to which a control is bound and determines how a control is resized with its parent.

public:
 virtual property System::Windows::Forms::AnchorStyles Anchor { System::Windows::Forms::AnchorStyles get(); void set(System::Windows::Forms::AnchorStyles value); };
public virtual System.Windows.Forms.AnchorStyles Anchor { get; set; }
member this.Anchor : System.Windows.Forms.AnchorStyles with get, set
Public Overridable Property Anchor As AnchorStyles

Valor de propiedad

Combinación bit a bit de los valores de AnchorStyles.A bitwise combination of the AnchorStyles values. Los valores predeterminados son Top y Left.The default is Top and Left.

Ejemplos

En el ejemplo de código siguiente Button se agrega un a un formulario y se establecen algunas de sus propiedades comunes.The following code example adds a Button to a form and sets some of its common properties. En el ejemplo se delimita el botón en la esquina inferior derecha del formulario para que mantenga su posición relativa a medida que se cambia el tamaño del formulario.The example anchors the button to the bottom-right corner of the form so it keeps its relative position as the form is resized. A continuación, se BackgroundImage establece y se cambia el tamaño del botón al mismo tamaño Imageque.Next it sets the BackgroundImage and resizes the button to the same size as the Image. A continuación, TabStop en el ejemplo se establece en TabIndex y se true establece la propiedad.The example then sets the TabStop to true and sets the TabIndex property. Por último, agrega un controlador de eventos para controlar el Click evento del botón.Lastly, it adds an event handler to handle the Click event of the button. Este ejemplo requiere que tenga un ImageList nombre. imageList1This example requires that you have an ImageList named imageList1.

   // Add a button to a form and set some of its common properties.
private:
   void AddMyButton()
   {
      // Create a button and add it to the form.
      Button^ button1 = gcnew Button;

      // Anchor the button to the bottom right corner of the form
      button1->Anchor = static_cast<AnchorStyles>(AnchorStyles::Bottom | AnchorStyles::Right);

      // Assign a background image.
      button1->BackgroundImage = imageList1->Images[ 0 ];

      // Specify the layout style of the background image. Tile is the default.
      button1->BackgroundImageLayout = ImageLayout::Center;

      // Make the button the same size as the image.
      button1->Size = button1->BackgroundImage->Size;

      // Set the button's TabIndex and TabStop properties.
      button1->TabIndex = 1;
      button1->TabStop = true;

      // Add a delegate to handle the Click event.
      button1->Click += gcnew System::EventHandler( this, &Form1::button1_Click );

      // Add the button to the form.
      this->Controls->Add( button1 );
   }
// Add a button to a form and set some of its common properties.
private void AddMyButton()
{
   // Create a button and add it to the form.
   Button button1 = new Button();

   // Anchor the button to the bottom right corner of the form
   button1.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);

   // Assign a background image.
   button1.BackgroundImage = imageList1.Images[0];

   // Specify the layout style of the background image. Tile is the default.
   button1.BackgroundImageLayout = ImageLayout.Center;
   
   // Make the button the same size as the image.
   button1.Size = button1.BackgroundImage.Size;

   // Set the button's TabIndex and TabStop properties.
   button1.TabIndex = 1;
   button1.TabStop = true;

   // Add a delegate to handle the Click event.
   button1.Click += new System.EventHandler(this.button1_Click);

   // Add the button to the form.
   this.Controls.Add(button1);
}
' Add a button to a form and set some of its common properties.
Private Sub AddMyButton()
   ' Create a button and add it to the form.
   Dim button1 As New Button()
   
   ' Anchor the button to the bottom right corner of the form
   button1.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
   
   ' Assign a background image.
   button1.BackgroundImage = imageList1.Images(0)

   ' Specify the layout style of the background image. Tile is the default.
   button1.BackgroundImageLayout = ImageLayout.Center
   
   ' Make the button the same size as the image.
   button1.Size = button1.BackgroundImage.Size
   
   ' Set the button's TabIndex and TabStop properties.
   button1.TabIndex = 1
   button1.TabStop = True

   ' Add a delegate to handle the Click event.
   AddHandler button1.Click, AddressOf Me.button1_Click
   
   ' Add the button to the form.
   Me.Controls.Add(button1)
End Sub

Comentarios

Utilice la Anchor propiedad para definir cómo se cambia automáticamente el tamaño de un control a medida que se cambia el tamaño del control primario.Use the Anchor property to define how a control is automatically resized as its parent control is resized. Al delimitar un control a su control principal, se garantiza que los bordes anclados permanecen en la misma posición con respecto a los bordes del control principal cuando se cambia el tamaño del control primario.Anchoring a control to its parent control ensures that the anchored edges remain in the same position relative to the edges of the parent control when the parent control is resized.

Puede anclar un control a uno o varios bordes de su contenedor.You can anchor a control to one or more edges of its container. Por ejemplo Form , si tiene un con un Button cuyo Anchor valor de Button propiedad se establece en Top y Bottom, se ajusta para mantener la distancia delimitada a los bordes superior e inferior del Forma medida Height que aumenta Form el de.For example, if you have a Form with a Button whose Anchor property value is set to Top and Bottom, the Button is stretched to maintain the anchored distance to the top and bottom edges of the Form as the Height of the Form is increased.

Nota

El Anchor y Dock propiedades son mutuamente excluyentes.The Anchor and Dock properties are mutually exclusive. Solo se puede establecer una a la vez y el último conjunto tiene prioridad.Only one can be set at a time, and the last one set takes precedence.

Notas a los desarrolladores de herederos

Al reemplazar la Anchor propiedad en una clase derivada, utilice la propiedad de Anchor la clase base para extender la implementación base.When overriding the Anchor property in a derived class, use the base class's Anchor property to extend the base implementation. De lo contrario, debe proporcionar toda la implementación.Otherwise, you must provide all the implementation. No es necesario reemplazar los get descriptores de acceso y set de la Anchor propiedad; solo se puede reemplazar uno si es necesario.You are not required to override both the get and set accessors of the Anchor property; you can override only one if needed.

Se aplica a

Consulte también: