Label.PreferredHeight Property

Definition

获取控件的首选高度。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

Property Value

Int32

控件的高度(以像素为单位),假定显示单行文本。The height of the control (in pixels), assuming a single line of text is displayed.

Attributes

Examples

下面的代码示例演示如何创建一个 Label 控件,该控件具有三维边框和使用 ImageListImageIndex 属性显示的图像。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

Remarks

此属性根据分配给控件的字体返回控件应为正确显示文本的高度。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.

Note

如果 Label 控件的 BorderStyle 属性设置为 BorderStyle.None,则由于缺少边框,PreferredHeight 属性返回的值将更大。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.

Applies to

See also