Compartir vía


Control.Size Propiedad

Definición

Obtiene o establece el alto y el ancho del control.

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

Valor de propiedad

Size que representa el alto y el ancho del control en píxeles.

Ejemplos

En el ejemplo de código siguiente se agrega un Button objeto a un formulario y se establecen algunas de sus propiedades comunes. En el ejemplo se ancla el botón a la esquina inferior derecha del formulario, por lo que mantiene su posición relativa a medida que se cambia el tamaño del formulario. A continuación, establece y BackgroundImage cambia el tamaño del botón al mismo tamaño que .Image A continuación, el ejemplo establece en TabStoptrue y establece la TabIndex propiedad . Por último, agrega un controlador de eventos para controlar el Click evento del botón. En este ejemplo se requiere que tenga un ImageList objeto denominado 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

Dado que la Size clase es un tipo de valor (Structure en Visual Basic, struct en Visual C#), se devuelve por valor, lo que significa que el acceso a la propiedad devuelve una copia del tamaño del control. Por lo tanto, ajustar las Width propiedades o Height del Size devuelto desde esta propiedad no afectará a Width ni Height al control . Para ajustar el Width control o Height , debe establecer la propiedad o Height del controlWidth, o bien establecer la Size propiedad con un nuevo Size.

Nota:

Para mantener un mejor rendimiento, no establezca el Size de un control en su constructor. El método preferido es invalidar la DefaultSize propiedad .

Nota:

En los sistemas Windows Server 2003, el tamaño de un Form elemento está restringido por el ancho máximo de píxeles y el alto del monitor.

Se aplica a

Consulte también