Control.Size Właściwość

Definicja

Pobiera lub ustawia wysokość i szerokość kontrolki.

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

Wartość właściwości

Obiekt Size reprezentujący wysokość i szerokość kontrolki w pikselach.

Przykłady

Poniższy przykład kodu dodaje obiekt Button do formularza i ustawia niektóre z jego typowych właściwości. Przykład zakotwiczy przycisk w prawym dolnym rogu formularza, aby zachować względną pozycję w miarę zmieniania rozmiaru formularza. Następnie ustawia BackgroundImage przycisk i zmienia rozmiar przycisku na taki sam rozmiar jak .Image W tym przykładzie ustawiono TabStop właściwość na true , a następnie ustawia TabIndex właściwość . Na koniec dodaje procedurę obsługi zdarzeń w celu obsługi Click zdarzenia przycisku. Ten przykład wymaga ImageList nazwy 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

Uwagi

Size Ponieważ klasa jest typem wartości (Structure w Visual Basic, struct w Visual C#), jest zwracana przez wartość, co oznacza, że uzyskanie dostępu do właściwości zwraca kopię rozmiaru kontrolki. Dlatego dostosowanie Width właściwości lub Height zwróconych Size z tej właściwości nie wpłynie na Width właściwość lub Height kontrolki. Aby dostosować kontrolkę Width lub Height , należy ustawić właściwość lub Height kontrolki Width albo ustawić Size właściwość przy użyciu nowego Sizeelementu .

Uwaga

Aby utrzymać lepszą wydajność, nie ustawiaj Size kontrolki w konstruktorze. Preferowaną metodą jest zastąpienie DefaultSize właściwości .

Uwaga

W systemach Windows Server 2003 rozmiar obiektu Form jest ograniczony przez maksymalną szerokość i wysokość monitora.

Dotyczy

Zobacz też