Control.Size Egenskap
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Hämtar eller anger kontrollens höjd och bredd.
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
Egenskapsvärde
Det Size som representerar kontrollens höjd och bredd i bildpunkter.
Exempel
I följande kodexempel läggs ett Button till i ett formulär och några av dess gemensamma egenskaper anges. Exemplet fäster knappen i formulärets nedre högra hörn så att den behåller sin relativa position när formuläret ändras. Därefter ställer den BackgroundImage in och ändrar storlek på knappen till samma storlek som Image. Exemplet anger TabStop sedan egenskapen till true och TabIndex . Slutligen lägger den till en händelsehanterare för att hantera Click händelsen för knappen. Det här exemplet kräver att du har namnet ImageListimageList1.
// 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
Kommentarer
Eftersom klassen Size är en värdetyp (Structure i Visual Basic, struct i Visual C#), returneras den med värde, vilket innebär att åtkomst till egenskapen returnerar en kopia av kontrollens storlek. Om du justerar Width egenskaperna eller Height för den Size som returneras från den här egenskapen påverkas Width inte kontrollens eller Height . Om du vill justera kontrollens eller Width måste du ange kontrollens Height eller Width egenskapens eller ange Height egenskapen med en ny Size.Size
Note
För att upprätthålla bättre prestanda ska du inte ange Size en kontroll i konstruktorn. Den bästa metoden är att åsidosätta DefaultSize egenskapen.
Note
På Windows Server 2003-system begränsas storleken på en Form av bildskärmens maximala pixelbredd och höjd.