Partager via


Control.Size Propriété

Définition

Obtient ou définit la hauteur et la largeur du contrôle.

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

Valeur de propriété

Qui Size représente la hauteur et la largeur du contrôle en pixels.

Exemples

L’exemple de code suivant ajoute un Button formulaire et définit certaines de ses propriétés communes. L’exemple ancre le bouton dans le coin inférieur droit du formulaire afin qu’il conserve sa position relative lorsque le formulaire est redimensionné. Ensuite, il définit le BackgroundImage bouton et redimensionne le bouton sur la même taille que le Image. L’exemple définit ensuite la TabStoptrue valeur et définit la TabIndex propriété. Enfin, il ajoute un gestionnaire d’événements pour gérer l’événement Click du bouton. Cet exemple nécessite que vous ayez un ImageList nom 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

Remarques

Étant donné que la classe est un type valeur (Structureen Visual Basic, struct en Visual C#), elle est retournée par valeur, ce qui signifie que l’accès Size à la propriété retourne une copie de la taille du contrôle. Par conséquent, l’ajustement des Width propriétés ou SizeHeight des propriétés retournées à partir de cette propriété n’affecte pas le Width ou Height le contrôle. Pour ajuster le Width ou Height le contrôle, vous devez définir la propriété ou Height le contrôleWidth, ou définir la Size propriété avec un nouveau Size.

Note

Pour maintenir de meilleures performances, ne définissez pas le Size contrôle dans son constructeur. La méthode préférée consiste à remplacer la DefaultSize propriété.

Note

Sur les systèmes Windows Server 2003, la taille d’un Form système est limitée par la largeur et la hauteur maximales de pixels du moniteur.

S’applique à

Voir aussi