Label.PreferredHeight Label.PreferredHeight Label.PreferredHeight Label.PreferredHeight Property

定義

取得控制項的慣用高度。Gets the preferred height of the control.

public:
 virtual property int PreferredHeight { int get(); };
[System.ComponentModel.Browsable(false)]
public virtual int PreferredHeight { get; }
member this.PreferredHeight : int
Public Overridable ReadOnly Property PreferredHeight As Integer

屬性值

控制項的高度 (單位為像素),假設所顯示的是單行文字。The height of the control (in pixels), assuming a single line of text is displayed.

範例

下列程式碼範例示範如何建立Label具有3d 框線的控制項,以及ImageList使用和ImageIndex屬性顯示的影像。The following code example demonstrates how to create a Label control that has a three-dimensional border and an image displayed using the ImageList and ImageIndex properties. 控制項也有一個標題,其中指定了助憶鍵字元。The control also has a caption with a mnemonic character specified. 範例程式碼會使用PreferredHeightPreferredWidth Label屬性,適當地將控制項的大小調整為顯示的表單上。The example code uses the PreferredHeight and PreferredWidth properties to properly size the Label control on the form on which it is displayed. 這個範例要求ImageList已建立並命名為 imageList1,且已載入兩個映射。This example requires that an ImageList has been created and named imageList1 and that it has loaded two images. 此範例也會要求程式碼必須在將System.Drawing命名空間加入其程式碼的表單中。The example also requires that the code is within a form that has the System.Drawing namespace added to its code.

public:
   void CreateMyLabel()
   {
      // Create an instance of a Label.
      Label^ label1 = gcnew Label;
      
      // Set the border to a three-dimensional border.
      label1->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;
      // Set the ImageList to use for displaying an image.
      label1->ImageList = imageList1;
      // Use the second image in imageList1.
      label1->ImageIndex = 1;
      // Align the image to the top left corner.
      label1->ImageAlign = ContentAlignment::TopLeft;
      
      // Specify that the text can display mnemonic characters.
      label1->UseMnemonic = true;
      // Set the text of the control and specify a mnemonic character.
      label1->Text = "First &Name:";
      
      /* Set the size of the control based on the PreferredHeight and PreferredWidth values. */
      label1->Size = System::Drawing::Size( label1->PreferredWidth, label1->PreferredHeight );
      
      //...Code to add the control to the form...
   }
public void CreateMyLabel()
{
   // Create an instance of a Label.
   Label label1 = new Label();

   // Set the border to a three-dimensional border.
   label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
   // Set the ImageList to use for displaying an image.
   label1.ImageList = imageList1;
   // Use the second image in imageList1.
   label1.ImageIndex = 1;
   // Align the image to the top left corner.
   label1.ImageAlign = ContentAlignment.TopLeft;

   // Specify that the text can display mnemonic characters.
   label1.UseMnemonic = true;
   // Set the text of the control and specify a mnemonic character.
   label1.Text = "First &Name:";
   
   /* Set the size of the control based on the PreferredHeight and PreferredWidth values. */
   label1.Size = new Size (label1.PreferredWidth, label1.PreferredHeight);

   //...Code to add the control to the form...
}

Public Sub CreateMyLabel()
    ' Create an instance of a Label.
    Dim label1 As New Label()
       
    ' Set the border to a three-dimensional border.
    label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
    ' Set the ImageList to use for displaying an image.
    label1.ImageList = imageList1
    ' Use the second image in imageList1.
    label1.ImageIndex = 1
    ' Align the image to the top left corner.
    label1.ImageAlign = ContentAlignment.TopLeft
     
    ' Specify that the text can display mnemonic characters.
    label1.UseMnemonic = True
    ' Set the text of the control and specify a mnemonic character.
    label1.Text = "First &Name:"
       
    ' Set the size of the control based on the PreferredHeight and PreferredWidth values. 
    label1.Size = New Size(label1.PreferredWidth, label1.PreferredHeight)

    '...Code to add the control to the form...
End Sub

備註

這個屬性會根據指派給控制項的字型,傳回控制項應依據的高度,以適當地顯示文字。This property returns the height that the control should be in order to properly display text, based on the font assigned to the control. 您可以搭配PreferredWidth屬性使用這個屬性,以確保Label控制項中的文字會正確顯示。You can use this property along with the PreferredWidth property to ensure that the text in the Label control is displayed properly. 您可以使用AutoSize屬性,根據文字和字型大小,自動調整Label控制項的高度和寬度。You can use the AutoSize property to automatically adjust the height and the width of the Label control, based on the text and font size.

注意

如果Label控制項BorderStyle的屬性設定為BorderStyle.NonePreferredHeight屬性傳回的值將會因為缺少框線而變大。If the BorderStyle property of the Label control is set to BorderStyle.None, the value returned by PreferredHeight property will be larger due to the lack of borders.

適用於

另請參閱